Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我正在尝试将字符串替换text9 text text为9only。
text9 text text
9
下面的代码正在运行,但按字面意思返回{1}
{1}
$("body").html($("body").html().replace(/text([0-9]+)[ \t]text text/g,'{1}'));
{1}我希望它[0-9]+从搜索中返回,而不是按字面意思返回。
[0-9]+
您将如何使用正则表达式来做到这一点?
替换你的代码
yourstring.replace(/text([0-9]+)[ \t]text text/g, '{1}');
有了这个:
yourstring.replace(/text([0-9]+)[ \t]text text/g, '$1');
您的代码将如下所示:
$('body').html(function (_, html) { return html.replace(/text([0-9]+)[ \t]text text/g, '$1'); });
小提琴演示