Regular expressions that remove only the first "/"

I am new to regex and can't figure out how to solve this:

I need regular expressions that only allow numbers, lettersand /. I wrote this:

/[^a-zA-Z0-9/]/g

I think it’s possible to disable the first one /, but I don’t know how to do it.

therefore #/register/step1it becomesregister/step1

Who knows how I can get this result?

Thank!

+3
source share
3 answers

You can use non-global matching if the pattern is adjacent in the line:

var rx=/(([a-zA-Z0-9]+\/*)+)/;

var s='#/register/step1';

var s1=(s.match(rx) || [])[0];


alert(s1)>>>  returned value: (String) "register/step1"
+2
source

edit: , , , ( ) , strpos (), substr() - . preg_replace() , ,

0
"/register/step1".match(/[a-zA-Z0-9][a-zA-Z0-9/]*/); // ["register/step1"]

\ w is equivalent to [^ A-Za-z0-9_], therefore:

"/register/step1".match(/\w[\w/]*/); // ["register/step1"]
0
source

All Articles