Jquery正则表达式替换为搜索

时间:2013-07-10 12:46:10

标签: jquery regex replace

我正在尝试将字符串text9 text text仅替换为9

以下代码正在运行,但字面意思返回{1}

$("body").html($("body").html().replace(/text([0-9]+)[ \t]text text/g,'{1}'));

我希望它从搜索中返回{1},而不是按字面[0-9]+返回。

你怎么用Regex做到这一点?

1 个答案:

答案 0 :(得分:1)

替换您的代码

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');
});

FIDDLE DEMO