javascript - Regular expression for remove last n characters -


i have requirement remove last n characters string or remove 'page' particular string. eg:

var string = 'facebookpage';  expected output string = 'facebook' 

i remove 'page' string.

done using substring.

var str = "facebookpage"; str = str.substring(0, str.length - 4); 

could me find better way it.

regex this:

//str - string; //n - count of symbols, return  function(str, n){   var re = new regexp(".{" + n + "}","i");   return str.match(re); }; 

edit:

for remove last n characters:

var re = new regexp(".{" + n + "}$","i"); return str.replace(re, ""); 

update:
use regex task, not way; example, avg runtime 100000 iterations:

str length solution = 63.34 ms regex solution      = 172.2 ms 

Comments

Popular posts from this blog

html - Firefox flex bug applied to buttons? -

html - Missing border-right in select on Firefox -

python - build a suggestions list using fuzzywuzzy -