0

I have a javascript regex

   Value.match(/[A-Za-z0-9\-\,\.\(\)/]/)

This gives me 1 if a string contains alphabets, numbers, hyphen, comma, dot or braces; if any other character is found it gives 0.

When I apply same regex in PHP it is not working. Why?

1
  • no code provided along with the powerful description of is not working... do you really expect solid answers?? Commented Dec 16, 2012 at 0:41

1 Answer 1

2

You don't need to escape characters inside [] so you can try this /[A-Za-z0-9,.()]/ or even this one /[\w,.()]/ but if you want to check that the string contains only those characters that regex won't do, try:

/^[\w,.()]+$/

I noticed that you also have /. Is that intentional or a mistake, because you don't mention it in the question...

Sign up to request clarification or add additional context in comments.

5 Comments

In javascript / is used to mark regexps while you have to put the regex into a string in php. so this might be the main problem. but your answer has a good point. +1
^ Right, that could be the problem too. We need to see php code.
Thank you for your help !! Its working !! :-) !! I used \/ for checking / !! Need two more help !! One it checks a string containing only a word what if it is a sentence with spaces ex "there is my error". Two can you guide me to a resource link where i can learn regex in php !! Thank you once again
Regex are pretty much the same across languages. I think the best resource is regular-expressions.info. It's language agnostic but it it'll tell you if something differs in other languages.
Thanks really appreciate you help !! :-) !! And your link was awesome !!

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.