javascript正则表达式 - 替换字符串停用词

时间:2013-03-16 17:27:47

标签: javascript regex string

我正在尝试编写一个从字符串中删除特定单词的函数。

以下代码在句子的最后一个单词之前正常工作,因为它后面没有我的正则表达式查找的空格。

如何捕捉空格后面没有的最后一个单词?

JS Fiddle

function stopwords(input) {

var stop_words = new Array('a', 'about', 'above', 'across');

console.log('IN: ' + input);

stop_words.forEach(function(item) {
    var reg = new RegExp(item +'\\s','gi')

    input = input.replace(reg, "");
});

console.log('OUT: ' + input);
}

stopwords( "this is a test string mentioning the word across and a about");

2 个答案:

答案 0 :(得分:2)

您可以使用word boundary marker

var reg = new RegExp(item +'\\b','gi')

答案 1 :(得分:1)

假设我在单词

上传递sea
stopwords( "this is a test string sea mentioning the word across and a about");

会将sea缩减为se

function stopwords(input) {

  var stop_words = ['a', 'about', 'above', 'across'];

  console.log('IN: ' + input);

  // JavaScript 1.6 array filter
  var filtered  = input.split( /\b/ ).filter( function( v ){
        return stop_words.indexOf( v ) == -1;
  });

  console.log( 'OUT 1 : ' + filtered.join(''));

  stop_words.forEach(function(item) {
      // your old : var reg = new RegExp(item +'\\s','gi');
      var reg = new RegExp(item +'\\b','gi'); // dystroy comment

      input = input.replace(reg, "");
  });

  console.log('OUT 2 : ' + input);
}

stopwords( "this is a test string sea mentioning the word across and a about");

有输出

IN: this is a test string sea mentioning the word across and a about

OUT 1 : this is  test string sea mentioning the word  and  

OUT 2 : this is  test string se mentioning the word  and