0

I have been doing some testing with the differences between JavaScript and PHP regular expressions and "I believe" that the following 2 expressions are pretty much exactly the same thing, but I would like to ask the gurus here to confim this or not before I add these to my production code.

PHP Version

preg_replace('/[^a-zA-Z0-9\.\-\_]/', '_', $str);

JavaScript Version

str.replace(/[^a-zA-Z\d\.\-\_]/g, "_");

Thx,
Dave Keltz

2
  • I was speaking of functionality like miniteck pointed out not so much how they were formatted. Thx for the reply tho. Commented Mar 5, 2012 at 4:40
  • Sorry, I think I was wrong about PHP syntax anyway, so I'll delete my previous comment so it doesn't lead people astray. Commented Mar 5, 2012 at 4:47

1 Answer 1

1

Yes, they're identical in functionality. Of course, you can shorten both to this:

/[^\w\.-]/

PHP Demo
JavaScript Demo

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

1 Comment

Thx, I was pretty sure they were, but I just needed someone else with more knowledge then myself to confirm it. As for shortining things goes I kinda like it the way it is now because it's easier for me to understand if I ever have to not edit it at a later point in time.

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.