本文介绍了一切除了正则表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试删除数字的所有实例,除了我提供的数字。这是我目前所拥有的:
I am trying to remove all the instances of a number except for a number that I supply. This is currently what I have:
var ar = '10,13,9,8,13,10,5,3,1,9,5,8,10,2,6';
var removed = ar.replace(new RegExp('[^(' + 10 + ')]', 'g'), '')
结果是101110110,但我需要它除去那些完全匹配的所有内容。所以结果应该是101010,因为字符串中只有310。
提前致谢,
-Dom
The result of this is "101110110", but I need it to remove all except those with an exact match. So the result should be ""101010" as there are only 3 "10"'s in the string.
Thanks in advance,
-Dom
推荐答案
var removed = ar.match(new RegExp('\\b'+10+'\\b','g')).join('');
问候,
Niral Soni
Regards,
Niral Soni
这篇关于一切除了正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!