Reputation: 322
I'm new to Regular expressions and can't seem to find out how I have to solve this:
I need a regular expressions that "allows" only numbers
, letters
and /
. I wrote this:
/[^a-zA-Z0-9/]/g
I think it's possible to strip the first /
off, but don't know how.
so #/register/step1
becomes register/step1
Who knows how I could get this result?
Thanks!
Upvotes: 3
Views: 94
Reputation: 8520
"/register/step1".match(/[a-zA-Z0-9][a-zA-Z0-9/]*/); // ["register/step1"]
\w is Equivalent to [^A-Za-z0-9_], so:
"/register/step1".match(/\w[\w/]*/); // ["register/step1"]
Upvotes: 1
Reputation: 2101
edit: Don't know why i didn't suggest this first, but if you're simply enforcing the pattern rather than replacing, you could just replace that slash (if it exists) before checking the pattern, using strpos(), substr(), or something similar. If you are using a preg_replace() already, then you should look at the examples on the function docs, they are quite relevant
Upvotes: 0
Reputation: 104790
You can use a non-global match, if the pattern is contiguous in the string:
var rx=/(([a-zA-Z0-9]+\/*)+)/;
var s='#/register/step1';
var s1=(s.match(rx) || [])[0];
alert(s1)>>> returned value: (String) "register/step1"
Upvotes: 2