0

I need information about finding a word and replacing it with regular expressions in javascript.

2 Answers 2

3

You can use \b to specify a word boundary. Just put them around the word that you want to replace.

Example:

var s = "That's a car.";
s = /\ba\b/g.replace(s, "the");

The variable s now contains the string "That's the car". Notice that the "a" in "That's" and "car" are unaffected.

Sign up to request clarification or add additional context in comments.

Comments

0

Since you haven't really asked a question, the best I can do is point you here:

http://www.w3schools.com/jsref/jsref_obj_regexp.asp

And tell you also that the replace method for a string is replace, as in:

var myStr = "there is a word in here";
var changedStr = myStr.replace(/word/g, "replacement");

4 Comments

@Renesis: please, w3schools.com is awful, post a better link. How about this one: developer.mozilla.org/en/JavaScript/Reference/Global_Objects/…
@Sean Patrick Floyd thanks for the other link. Not sure I agree that it's awful, even if it's usually light on explanation, it is a succinct reference for syntax.
@Renesis you can find some discussion about w3schools in this question: stackoverflow.com/questions/4312945/…
@Sean - I don't see any discussion there - and again, when googling I don't often come up with any top 10 results that are as succinct as w3schools.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.