Javascript: regular expression

Posted by Dmitry Nesteruk on Stack Overflow See other posts from Stack Overflow or by Dmitry Nesteruk
Published on 2010-05-07T13:45:59Z Indexed on 2010/05/07 13:48 UTC
Read the original article Hit count: 197

Filed under:

I need to replace a substring from some string. I've already created corrected code for doing it. But I amn't sure is it best way. Please, see code below:

var str = 'test ruby,ruby on rails,ruby,'
var substr = 'ruby';
var reg = new RegExp(',' + substr + ',|^' + substr + ',', 'gi');
str.replace(reg, ','); //returns "test ruby,ruby on rails,"

© Stack Overflow or respective owner

Related posts about JavaScript