Helpful Information
 
 
Category: Regex Programming
Javascript replace

I have text which can look like any of these:

5 (53.52)
25(2.35)
Hello (6.25)

I need to, in javascript, get the text inside of the parentheses. The rest is garbage. I know how to match the text with a regex, but not find it for use in .replace();

Perhaps there is a better method then .replace for this? Any help would be greatly appreciated.

I guess I could use match


txt = string.match('/\(.*?\)/');

Although that regex returns null

That code snippet isn't working as you expect because the forward slashes are used literally because it's a string, so it's silently being converted to a regexp object.

I might do it like this: (the join, replace, and split method calls are to remove the parentheses.)

txt = string.match(/\([^\(\)]+\)/g).join('').replace(/\(|\)$/g,'').split(/\)/);










privacy (GDPR)