我正在尝试匹配不被字符串中的点分隔的单词。
因为Javascript中没有任何落后之处,所以我一直在为此苦苦挣扎,无法正常工作。
测试字串1:'one two three.four five six.seven eight'
应匹配:'one', 'two', 'five', 'eight'
测试字符串2:'one.two three four.five six seven.eight'
应匹配:'three', 'six'
更新:
测试字符串3:'one.two three four five six seven.eight'
应该匹配:'three', 'four', 'five', 'six'
到目前为止,我已经( |^)(\w+)( |$)
对teststring 2起作用了,但是没有匹配'two'
。
有什么办法可以使用正则表达式执行此操作,还是应该将其拆分为数组然后遍历?
与正则表达式 ( |^)\w+(?= |$)
'one two three.four five six.seven eight'.replace(/( |^)\w+(?= |$)/g, '$1TEST')
或不使用正则表达式(可能更具可读性)
'one two three.four five six.seven eight'.split(' ').map(function(item) {
if(item.indexOf('.') < 0)
return 'TEST';
return item;
}).join(' ')
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句