Ad

How To Replace *( In A String

- 1 answer

My string is Mario *(cart) and I need to replace the *( in order to get Mario (cart). I don't want to replace just the asterisk, but the asterisk followed by an open parenthesis using global in a regex.

Here the code I'm trying to fix:

var str = 'Mario *(cart)';
str=str.replace(/\*\(/g,'(');

CORRECTION The regex works fine, but do not work when I leave the parenthesis without a value.

I made this code to answer at Creating dynamic formula

jsFiddle: https://jsfiddle.net/qpsh940m/

Ad

Answer

Following http://regexr.com/ your regex is fine.

Following javascript replace docs you are using correctly .replace

There is nothing wrong in your code, i tested it on the javascript console (chrome) and its worked right, here is the log:

var str = 'Mario *(cart)';
undefined
alert(str)
undefined
var str2 = str.replace(/\*\(/g,'(');
undefined
console.log(str2)
VM650:2 Mario (cart)
undefined

Where are you trying to implement it?

Its dont matter where you read it, if its right and complete.

Ad
source: stackoverflow.com
Ad