javascript - How to restrict the first char of a string using regex -


i working javascript, restrict first char of string. have following regex avoid especial characters in whole string.

/[^0-9_a-za-z\s]/g var text = 'foo#' var transformed = text.replace(/[^0-9_a-za-z%$#\s]/g, '');// 'foo' 

but additionally don't want number @ beginning of string yes in other position of string, regex that?

var text = '1fo4565o' var transformed = text.replace([?], '');// 'fo4565o' 

some useful sites doing kind of work https://regex101.com/ or http://www.regexr.com/ show effects on target text, tell each part doing, providing cheat-sheet.

if want replace first character, anchor beginning ^ character. \d+ digit character repeated @ least once. | match group or next group. combined get:

   /^\d+|[^0-9_a-za-z%$#\s]/g 

which match digits repeated @ least once @ beginning of string or character isn't in set.


Comments

Popular posts from this blog

cakephp - simple blog with croogo -

How to group boxplot outliers in gnuplot -

bash - Performing variable substitution in a string -