reconbot
reconbot

Reputation: 5297

Javascript regular expression global replace on strings with variables

I'm trying to add soft hyphens to break up long urls and other strings in some plain text.

function breakLines(str, num){
    if(typeof num == 'undefined' || num == null){ num = 15;}
    regex = new RegExp('(\S{'+num+'})(\S{'+num+'})','g');
    return str.replace(regex, '$1­$2');
}

It works if I use the slash notation for the replace but this function above doesn't seem to work.

Upvotes: 0

Views: 221

Answers (1)

reconbot
reconbot

Reputation: 5297

I didn't escape the \ next the the '\S's. Works fine now.

Upvotes: 1

Related Questions