regex - JavaScript to search a string and brace all capitalzied tokens -


i need javascript search string capitalized tokens, excluding first, , replace of them bracketed token.

e.g.

"rose harbor in bloom: novel" -> "rose {harbor} in {bloom}: {a} {novel}" "in america: novel" -> "in {america}: novel" "what else. in country" -> "what else. {in} country" 

you can use matched string in replacements using $&.
also, \b specifies word boundary , [a-z] specify capital characters.
*? attempts make match few characters possible

so want .replace(/\b[a-z].*?\b/g, '{$&}')

so example:

"a string yes?".replace(/\b[a-z].*?\b/g, '{$&}') 

returns

"a {string} {good} yes?" 

to exclude first token you'll have little creative;

function surroundcaps(str) {     //get first word     var temp = str.match(/^.+?\b/);     //if word found     if (temp) {         temp = temp[0];         //remove string         str = str.substring(temp.length);     }     else         temp = '';      str = str.replace(/\b[a-z].*?\b/g, '{$&}');     return temp + str; }  surroundcaps('string yes'); //'string {good} {yes}' 

Comments

Popular posts from this blog

java - activate/deactivate sonar maven plugin by profile? -

python - TypeError: can only concatenate tuple (not "float") to tuple -

java - What is the difference between String. and String.this. ? -