Javascript 找到短语时替换整个句子
我正在寻找一个JavaScript正则表达式,它会在一个句子中查找一个短语,比如“似乎是新的”,如果它找到了这个阶段,它会用“似乎”替换整个句子。因此,下面的所有句子都将替换为“似乎” 我如何摆脱“你看起来很新”的信息?Javascript 找到短语时替换整个句子,javascript,regex,Javascript,Regex,我正在寻找一个JavaScript正则表达式,它会在一个句子中查找一个短语,比如“似乎是新的”,如果它找到了这个阶段,它会用“似乎”替换整个句子。因此,下面的所有句子都将替换为“似乎” 我如何摆脱“你看起来很新”的信息? 如何获得“你似乎是新来的”消息? 我如何阻止“你似乎是新的”消息出现 您可以这样使用函数: var newString = ( 'How do I get rid of the "You seem to be new" message?'
如何获得“你似乎是新来的”消息?
我如何阻止“你似乎是新的”消息出现 您可以这样使用函数:
var newString = (
'How do I get rid of the "You seem to be new" message?'
+ ' How do I get kill the "You seem to be new" message?'
+ ' How do I stop the "You seem to be new" message from appearing?'
).replace(/You seem to be new/g, "seem");
这就是你要找的吗
var str = 'How do I get rid of the "You seem to be new" message? How do I get kill the "You seem to be new" message? How do I stop the "You seem to be new" message from appearing?'
str = str.replace(/[^?!.]*You seem to be new[^?!.]*[?!.]/g,"seem");
console.log(str); // "seemseemseem"
另外,您可以看到,如果我输入一个不匹配的字符串,会发生什么情况:
var str = 'How do I get rid of the "You seem to be new" message? How do I get kill the "You seem to be new" message? This sentence doesn\'t seem to contain your phrase. How do I stop the "You seem to be new" message from appearing?'
str = str.replace(/[^?!.]*You seem to be new[^?!.]*[?!.]/g,"seem");
console.log(str); //seemseem This sentence doesn't seem to contain your phrase.seem
如果要替换句子,但保留相同的标点符号:
var str = 'How do I get rid of the "You seem to be new" message? How do I get kill the "You seem to be new" message? This sentence doesn\'t seem to contain your phrase. How do I stop the "You seem to be new" message from appearing?'
str = str.replace(/[^?!.]*You seem to be new[^?!.]*([?!.])/g," seem$1");
console.log(str); // seem? seem? This sentence doesn't seem to contain your phrase. seem?
使用regexp的替代方法是使用
indexOf
var str='如何摆脱“您似乎是新的”消息?\n\
如何获得“您似乎是新的”消息?\n\
我如何阻止“你似乎是新的”信息出现;
变量行=str.split('\n'),
搜索字符串='似乎是新的',
替换_字符串='seen';
对于(变量i=0,n=lines.length;i-1)
行[i]=替换_字符串;
警报('原始消息:\n'+str+'\n\n新消息:\n'+lines.join('\n'));
您是否计划浏览包含多个句子的段落,并选择并替换包含此短语的句子?或者句子是独立出现的?他们每个人都在换行吗?我的答案应该是任意一种方式,但可以根据这些问题的答案变得更灵活。我可能应该首先提供这些信息,而不是在已经发布了三个答案之后,但这里还有一点。我的页面上有一个帮助系统,上面有一个“What is your question?”字段,是用普通英语输入的。我使用一个正则表达式字符串将原始输入“规范化”为键“tokens”,并向它们呈现与大多数标记匹配的主题。上面的三个例句是他们可能会问(任何人,不是所有人)的问题,如果他们厌倦了新手的信息。我在寻找最好的方法来确认这就是他们的要求。杀伤力过大?或者实际上比string.replace方法更有效?这还取决于每个可能匹配的句子之间有换行符。@Jeroenningelbrechtstring.replace
很可能是优化的更好的解决方案,但是indexOf
非常有效。我想这取决于绳子的长度。为了简单起见,我会使用正则表达式。@JeroenIngelbrecht查看动态正则表达式的性能比indexOf
@smerny:不一定慢。您可以像str.split('.')一样轻松地打断您的行。
或者在那里实际使用regex(例如str.split(/\n/)
)。如果句子在它们自己的元素中,那么显然你根本不需要拆分,你只需要选择那些dom textNodes。OP想用“seem”替换整个句子,如果该句子中有匹配项。这正是它所做的。我加入了一个jsfiddle,我看到的只是匹配本身被替换,而不是包含该匹配的整个句子。问题是,它实际上返回了一个新字符串,所以原始字符串(如果它在var中)实际上没有更改。字符串操作总是这样,但OP似乎希望在新创建的字符串中替换包含某个短语的任何句子(“整个句子”)。
var str = 'How do I get rid of the "You seem to be new" message?\n\
How do I get kill the "You seem to be new" message?\n\
How do I stop the "You seem to be new" message from appearing?';
var lines = str.split('\n') ,
search_string = 'seem to be new' ,
replace_string = 'seem' ;
for ( var i=0,n=lines.length; i<n; i++ )
if ( lines[i].indexOf(search_string) > -1 )
lines[i] = replace_string ;
alert('original message: \n' + str + '\n\nnew message: \n' + lines.join('\n'));