当前位置 : 首页 » 互动问答 » 正文

Javascript正则表达式从字符串中删除多个单词

分类 : 互动问答 | 发布时间 : 2018-04-27 15:06:13 | 评论 : 2 | 浏览 : 22 | 喜欢 : 1

使用Javascript。 (注意有一个类似的post,但OP请求的Java,这是为Javascript )

我试图从整个字符串中删除单词列表而不循环(最好使用正则表达式)。< notranslate>这是我到目前为止,并删除了一些单词,但不是全部。有人可以帮助确定我在做什么我的RegEx函数错了吗?

/ /删除所有单词的实例阵列   var removeUselessWords = function(txt){ var uselessWordsArray =         [           “a”,“at”,“be”,“can”,“can not”,“could”,“couldnt”,           “do”,“does”,“how”,“i”,“in”,“is”,“many”,“much”,“of”,           “on”,“or”,“should”,“shouldnt”,“so”,“such”,“the”           “他们”,“他们”,“至”,“我们”,“我们”,“什么”,“谁”,“为什么”,“           “with”,“wont”,“would”,“wouldnt”,“you”         ]。 var expStr = uselessWordsArray.join(“|”); return txt.replace(new RegExp(expStr,'gi'),'');   }   var str =“这个人在公园里散步,这个人告诉我们在公园做我们需要做的事情”;      的console.log(removeUselessWords(STR)); //结果应该是:“人走公园,人告诉需要公园。”

   //Remove all instances of the words in the array
  var removeUselessWords = function(txt) {

	var uselessWordsArray = 
        [
          "a", "at", "be", "can", "cant", "could", "couldnt", 
          "do", "does", "how", "i", "in", "is", "many", "much", "of", 
          "on", "or", "should", "shouldnt", "so", "such", "the", 
          "them", "they", "to", "us",  "we", "what", "who", "why", 
          "with", "wont", "would", "wouldnt", "you"
        ];
			
	var expStr = uselessWordsArray.join(" | ");
	return txt.replace(new RegExp(expStr, 'gi'), ' ');
  }

  var str = "The person is going on a walk in the park. The person told us to do what we need to do in the park";
  
  console.log(removeUselessWords(str));

//The result should be: "person going walk park. person told need park."

回答(2)

  • 1楼
  • 三个时刻:

    • 使用|连接数组项目,不带空格
    • 将正则表达式替换组包含在括号中(...|...)
    • 指定单词边界\b以匹配单独的单词

     var removeUselessWords = function(txt){
        var uselessWordsArray =
            [
              “a”,“at”,“be”,“can”,“can not”,“could”,“couldnt”,
              “do”,“does”,“how”,“i”,“in”,“is”,“many”,“much”,“of”,
              “on”,“or”,“should”,“shouldnt”,“so”,“such”,“the”
              “他们”,“他们”,“至”,“我们”,“我们”,“什么”,“谁”,“为什么”,“
              “with”,“wont”,“would”,“wouldnt”,“you”
            ]。
    
    var expStr = uselessWordsArray.join(“|”);
    返回txt.replace(new RegExp('\\ b('+ expStr +')\\ b','gi'),'')
                        .replace(/ \ s {2,} / g,'');
      }
    
    var str =“这个人在公园里散步,这个人告诉我们在公园做我们需要做的事情”;
      
    的console.log(removeUselessWords(STR));

  • 2楼
  • 可能这是你想要的:

     //移除数组中所有单词的所有实例
      var removeUselessWords = function(txt){
    
    var uselessWordsArray =
            [
              “a”,“at”,“be”,“can”,“can not”,“could”,“couldnt”,
              “do”,“does”,“how”,“i”,“in”,“is”,“many”,“much”,“of”,
              “on”,“or”,“should”,“shouldnt”,“so”,“such”,“the”
              “他们”,“他们”,“至”,“我们”,“我们”,“什么”,“谁”,“为什么”,“
              “with”,“wont”,“would”,“wouldnt”,“you”
            ]。
    
    var expStr = uselessWordsArray.join(“\\ b | \\ b”);
    返回txt.replace(new RegExp(expStr,'gi'),'').trim()。replace(/ + / g,'');
      }
    
      var str =“这个人在公园里散步,这个人告诉我们在公园做我们需要做的事情”;
      
      的console.log(removeUselessWords(STR));
    
    //结果应该是:“人走公园,人告诉需要公园。”

相关阅读:

How to move an element into another element?

Set HTTP header for one request

How to split one string into multiple strings separated by at least one space in bash shell?

What is the best regular expression to check if a string is a valid URL?

Remove duplicates from an array of objects in JavaScript

Can I call jquery click() to follow an <a> link if I haven't bound an event handler to it with bind or click already?

Check whether a string matches a regex in JS

Check whether a string matches a regex in JS

jQuery event to trigger action when a div is made visible

Check if user is using IE with jQuery