简体   繁体   English

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

[英]Find Exact Match From Page Using Javascript Regex

How to find exact match from my whole html.Below is the detail explanation. 如何从我的整个html中找到完全匹配。下面是详细说明。 Suppose i have html like below: 假设我有html如下:

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

Here i want to replace 'number' word. 在这里,我想替换'数字'一词。 i can do this using .replace(/number/g,'newnumber') but by using this it is also changing value in 'This is my linenumber' statement, it will also convert this statement to 'This is my linenewnumber'. 我可以使用.replace(/number/g,'newnumber')来做到这一点.replace(/number/g,'newnumber')但是通过使用它,它也会改变'This is my linenumber'语句中的值,它也会将此语句转换为'This is my linenewnumber'。

I don't want to do it. 我不想这样做。 I only need to change where there is single 'number',not with any word. 我只需要更改单个“数字”的位置,而无需更改任何单词。

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

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

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

May be this help you with jQuery: 也许这可以帮助你使用jQuery:

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

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

});
});

or this with javascript 或者用javascript

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

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

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