Find Exact Match From Page Using Javascript Regex
How to find exact match from my whole html.Below is the detail explanation. Suppose i have html like below:
<html>
<body>
.....
<table>
<tr>
<td>
This is my linenum开发者_JS百科ber
</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'.
I don't want to do it. I only need to change where there is single 'number',not with any word.
You want to match word boundaries using \b
:
.replace(/\bnumber\b/g, 'newnumber');
Try .replace(/\bnumber\b/g,'newnumber')
May be this help you with jQuery:
$(function() { $("table tr td").each(function(){ if($(this).text().trim() =='number') { $(this).text('newtext'); } }); });
or this with javascript
.replace(/\bnumber\b/g, 'newnumber');
精彩评论