Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

match end of line javascript regex

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.

like image 583
dr jerry Avatar asked Nov 07 '10 20:11

dr jerry


1 Answers

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.

like image 136
Vivin Paliath Avatar answered Sep 23 '22 11:09

Vivin Paliath