JQuery replace text between parentheses

I have a requirement to replace text between () in a string.

+2


a source to share


3 answers


"string()".replace(/\(.*?\)/, "replacement")

      



+18


a source


You can use regex - this is not jQuery, but part of JavaScript:

var s = "hello (there)";
s = s.replace(/\(.*?\)/, 'world');

      

For more than one pair:



s = s.replace(/\(.*?\)/g, 'world');

      

It won't work if there are more parentheses in the parentheses, mind you; Another option - use /\(.*\)/

for capturing from first to last - "a (b (c) d)"

→ "a world"

, but the same goes for "a (b) c (d)"

.

+11


a source


Use regular expressions instead of using JQuery: http://www.w3schools.com/jsref/jsref_replace.asp

This is the second example where you can specify a regex to target (s) and then replace the inner content ... something like /(.+)/i how regex should work.

NTN.

+1


a source







All Articles