Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent Leading Whitespace with RegEx

I'm trying to restrict review posting text with (at the present) the following RegEx:

^([a-zA-Z0-9 _\.\'\"\(\)\!\?\&\@]){1,}$

However, the text itself needs to contain whitespace in the form of ' ', but I don't want to allow the first sentence to have whitespace, i.e. the post cannot have leading whitespace. For an example, (I'm going to use '_' to represent ' ' in the following example) the form would allow:

This is a sentence.

But it would NOT allow:

(Using '_' to represent ' ')

_This is a sentence

__This is a sentence

etc.

I tried to work with negations, but I just don't understand RegEx well enough to grasp how to do this. This whole field is just not sitting well with me, so hopefully someone can help. Thanks in advance!

NOTE: I need this in RegEx because I have a live-feed validator telling the user that his/her input is valid/invalid. Another option that will allow me this real-time awareness will be sufficient for my purposes, if you happen to think of something better.

like image 962
Matt Avatar asked Dec 10 '22 05:12

Matt


1 Answers

You can also use a negative lookahead:

^(?!\s)([a-zA-Z0-9 _.'"()!?&@]){1,}$
like image 143
Jayendra Avatar answered Dec 27 '22 03:12

Jayendra