Javascript: regular expression
- by Dmitry Nesteruk
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,"