I'm probably doing something very stupid but I can't get following regexp to work in Javascript:
pathCode.replace(new RegExp("\/\/.*$","g"), "");
I want to remove // plus all after the 2 slashes.
Seems to work for me:
var str = "something //here is something more";
console.log(str.replace(new RegExp("\/\/.*$","g"), ""));
// console.log(str.replace(/\/\/.*$/g, "")); will also work
Also note that the regular-expression literal /\/\/.*$/g
is equivalent to the regular-expression generated by your use of the RegExp
object. In this case, using the literal is less verbose and might be preferable.
Are you reassigning the return value of replace
into pathCode
?
pathCode = pathCode.replace(new RegExp("\/\/.*$","g"), "");
replace
doesn't modify the string object that it works on. Instead, it returns a value.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With