user1276171
user1276171

Reputation: 75

JavaScript regex not working for PHP

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?

Upvotes: 0

Views: 192

Answers (1)

elclanrs
elclanrs

Reputation: 94101

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...

Upvotes: 2

Related Questions