繁体   English   中英

使用Javascript Regex从页面中找到完全匹配

[英]Find Exact Match From Page Using Javascript Regex

如何从我的整个html中找到完全匹配。下面是详细说明。 假设我有html如下:

<html>
<body>
.....
<table>
<tr>
<td>
This is my linenumber
</td>
<td>
number
</td>
</tr>
</table>
.....
</body>
</html>

在这里,我想替换'数字'一词。 我可以使用.replace(/number/g,'newnumber')来做到这一点.replace(/number/g,'newnumber')但是通过使用它,它也会改变'This is my linenumber'语句中的值,它也会将此语句转换为'This is my linenewnumber'。

我不想这样做。 我只需要更改单个“数字”的位置,而无需更改任何单词。

您想要使用\\b来匹配单词边界

.replace(/\bnumber\b/g, 'newnumber');

试试.replace(/\\bnumber\\b/g,'newnumber')

也许这可以帮助你使用jQuery:

$(function() {
$("table tr td").each(function(){

    if($(this).text().trim() =='number')
    {
    $(this).text('newtext');
    }

});
});

或者用javascript

.replace(/\bnumber\b/g, 'newnumber');

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM