0

鉴于:

Ya its a great day.

查找(在 1 个正则表达式语句中同时执行)

Ya => Ya + cat
great => great + cat

我怎么能做这样的事情?

"Ya its a great day".replace(/found=(Ya|great)/g, found + " cat");

输出:

Ya cat its a great cat day.
4

2 回答 2

1
"Ya its a great day".replace(/(Ya|great)/g,  "$1 cat");

输出:

Ya cat its a great cat day     

现场演示

@Pointy文档

于 2013-11-04T17:27:06.713 回答
0

您可以使用引用来引用找到的匹配:$1 $2 等等

"Ya its a great day".replace(/(Ya|great)/g, "$1 cat");
于 2013-11-04T17:27:22.517 回答