Shorthand javascript regex .replace -


i trying write code-golf in used javascript's regex .replace() method , second parameter passed anonymous function .replace(/(exp)/,function(x){return x.touppercase();}) which, understand, way needs done if u using matched sub-string determine replace.

at least thats understood from here.

is there shorter way this? eliminating function(x){return , ;}?

i saw code has replace without function call .replace(/([\[\]])/g, '\\$1');.

technically can simplify code, assigning function variable:

<script> var u = function(x) {     return x.touppercase();    } </script> 

then use variable instead of anonymous function:

str.replace(/(exp)/, u); 

but moves code rather eliminates it. still, achieves desired brevity.

see jsfiddle


Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -

php - Accessing static methods using newly created $obj or using class Name -