7

I have a JS string which contain text between double quotes ("). I want to replace that text by another user-entered text by using RegEx but I do not know how to do that. For example:

Given string:

The book "A Farewell to Arms" will be published again.

New text: Gone with the Wind --> the above sentence will be

The book "Gone with the Wind" will be published again.

Could you please help me to solve this case? Sorry for this dummy question because I am new to JS RegEx.

1
  • Do you have problems with creating the expression or applying it in JavaScript? The expression is not different that what you us in other languages. Commented Aug 20, 2012 at 15:56

1 Answer 1

10

This should do it..Spare me for my typo mistakes.I am not a javascript dev

str.replace(/".*?"/,"\"Gone with the Wind\"");
Sign up to request clarification or add additional context in comments.

4 Comments

You could also use "[^"]*".
@FelixKling I was fishing around for my own problem today, saw this comment, tried it and it worked. Another Felix Kling win, thanks :)
Just a note that this will not work if your string contains escaped quotes.
it only finding one match?

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.