\b是匹配一个单词的边界 ([a-z]+) 是匹配一个到多个小写字母 \1是引用第一个括号的内容 /ig 不区分大小写,并且全局搜索
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>菜鸟教程(runoob.com)</title> </head> <body> <script> var str = "Is is the cost of of gasoline going up up"; var patt1 = /\b([a-z]+) \1\b/ig; document.write(str.match(patt1)); </script> </body> </html>
匹配结果
Is is,of of,up up