1

I am a newbie in java script regex. I need to validate one of the fields in web page in my project using java script regex. --The field should not allow any special characters except ("/"). --Also The field shouldn't allow any word starting with a "/" or ending with "/".But in between "/" is allowed. --Also there is a restriction that it should not allow more than one consecutive slash. ---The field should allow single characters to pass as well

Basically the words which are allowed are :-

data
data/sdsd
d/s/s/sss

those which are not allowing are:-

/data
or //data
or data/
or data////
or data/////data

I though of two regex 1. (^[\w]+.*[^/]$) i.e allow word starting with alphanumeric character & also checkit should not allow the word to end with "/".

I know it's not a good regex(in between it'll allow multple number of "/" also special characters are also not taken care of) but the main problem with it is that it rejects single character, so please if any one can help me in modifying this regex to allow with single characters, it'll be of great help

  1. [\w]+([/][\w]+)* i.e. i wanted to create a regex which will allow any number of repetition of alphanumberic characters but if it sees a slash make sure that it is followed by a alphanueric character. & i read in internet that "()" is used to group the characters so i thought ([/][\w]+)* will act as group which will allow:-

    /s, /sss , /dadada

And the metacharacter "*" will make sure that it will be repeated any number of times. But the problem with this is it is not grouping [/] and [\w]+, them rather it is allowing words ending with "/" Please if anyone can help me with this it'll be of great help. Thnaks in advance!!

1
  • Please use `` around blocks of code to make it easier to read. Commented Apr 11, 2014 at 17:43

3 Answers 3

4

This probably isn't the most efficient, but it should get you started. Javascript's regex engine doesn't support lookbehinds, so that makes it a little more complicated.

^([a-z]+([a-z]|\/(?![\/]))*[a-z]|[a-z])$

http://regex101.com/r/iA9kC6

You'll also probably want to use the case-insensitive modifier, so the result (with / delimiters) looks like this:

/^([a-z]+([a-z]|\/(?![\/]))*[a-z]|[a-z])$/i
Sign up to request clarification or add additional context in comments.

5 Comments

Good work +1, why did I forget about negative look aheads when trying to get rid of repeated /s.
Thanks for the quick reply. i didn't think of the or operator.Can you explain it. as in I want to understand how this regex is allowing
regex101 can do a better job of explaining it than I can in the comments field ;) In short though, it will match (one|ortheother)
Thanks for the quick reply. i didn't think of the or operator.Can you explain how this regex is allowing a/data as in how is it allowing multiple characters after a "/". even though u have just written \/(?![\/]))*[a-z] meaning check that letter following "/" is [a-z] but how is it allowing /aaaa more than one letter after "/"
Please have a look at the regex101 link in my answer - it explains (breaks down) the expression in detail.
1

I think will be help solve your problem. The lazy quantifier on the group and the beginning and ending references to help anchor the whole thing seem to work.

/^\w([//]?\w+)*?$/ig

2 Comments

This is good, but I think OP wanted letters only - \w includes numbers and underscore. You also don't need need the // twice in the character class, but you do need to escape it since you're using slash as a delimiter: [\/]. You also don't need the lazy quantifier with the * since * is lazy anyway. regex101.com/r/bO9kE4
@remus: "...since * is lazy anyway." -- No it isn't. You're right about not needing the ?, but it's because greediness is the behavior we want. You should get rid of the other ? too, but for a different reason. If the first \w+ (note the +; that's another error) runs out of things to match, and the next thing is not / or the end of the string, the match attempt should fail. The enclosing group is optional, but within the group the slash is required.
0

This should solve your Problem:

^(\w(([/]?\w)*\w)?)$

The regexp says:

A match always has to start with a word-character.

If there are more than 1 characters, the last one also has to be a word-character.

All chacaters in-between are either a /, followed by a word character, or only a single word-character, short: ([/]?\w)*

Adding a plus might give you some performance improvements:

^(\w(([/]?\w+)*\w)?)$

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.