我正在使用这样的正则表达式:
/o?n?e?t?w?o?t?h?r?e?e?/
有没有办法做到这一点:
/(one)?(two)?(three)?/ // it doesn't work
我想用一个正则表达式替换单词“one”、“two”和“three”。
最佳答案
要用同一个词替换“one”、“two”或“three”,请使用 RegExp,用竖线分隔。还要为全局搜索和替换添加 g
标志。
例子:
var string = "one fellow, two hands, three legs";
string = string.replace(/(one|two|three)/g, "--$1--");
alert(string); //--one-- fellow, --two-- hands, --three-- legs
关于javascript - 正则表达式,分组常量字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8678249/