Javascript RegEx从字符串

时间:2018-04-04 15:37:32

标签: javascript regex string

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

我试图在没有循环的情况下从整个字符串中删除单词列表(最好使用正则表达式)。

这是我到目前为止所做的,它删除了一些词,但不是全部。有人可以帮助确定我的RegEx功能出错吗?



   //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 个答案:

答案 0 :(得分:4)

三个时刻:

  • 使用|加入没有旁边空格的数组项
  • 将正则表达式替换组括在括号(...|...)
  • 指定单词边界\b以匹配单独的单词

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('\\b(' + expStr + ')\\b', 'gi'), ' ')
                    .replace(/\s{2,}/g, ' ');
  }

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));

答案 1 :(得分:1)

可能这就是你想要的:

   //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("\\b|\\b");
	return txt.replace(new RegExp(expStr, 'gi'), '').trim().replace(/ +/g, ' ');
  }

  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."