Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex to replace exact matching string

I want a regex expression to replace the string which exactly matches it.

For e.g : - var a = '@test @te @world @dimension'

I need to replace '@te' .

Since '@te' exists in @test as well so Replace statement is replacing the @test in my case.

So could anyone please let me know how can this be done.

Just the exact matching string needs to be replaced.

like image 481
Sumodh Nair Avatar asked Jun 20 '13 09:06

Sumodh Nair


People also ask

How do you match exact strings?

We can match an exact string with JavaScript by using the JavaScript string's match method with a regex pattern that has the delimiters for the start and end of the string with the exact word in between those.

Does string replace take regex?

The Regex. Replace(String, MatchEvaluator, Int32, Int32) method is useful for replacing a regular expression match if any of the following conditions is true: The replacement string cannot readily be specified by a regular expression replacement pattern.

What is $1 in regex replace?

For example, the replacement pattern $1 indicates that the matched substring is to be replaced by the first captured group.


1 Answers

This should work for you:

/\@te\b/
like image 75
jezzipin Avatar answered Sep 23 '22 07:09

jezzipin