I am new to regex. I need a regex for JavaScript code that allows special character * in the beginning of text input, but not anywhere else in the text.
example : it should allow *text,
it should not allow *text*abcd
I would need a similar regex for PHP as well.
I have tried using “/^\s*/”, but this doesn’t work. Anyways I dont have anyidea of regex. I have started learning it.
Thanks
Dora
Part of learning how to use regex is knowing when you don’t need it. In this case a solution without regex would suffice, for PHP:
For JavaScript:
It basically finds the last position of the special character; if it appears in the string and is not the first character, the code inside the condition gets executed.