Matching Regular Expression in Javascript and PHP problem...

Posted by Frankie on Stack Overflow See other posts from Stack Overflow or by Frankie
Published on 2010-03-19T16:37:55Z Indexed on 2010/03/19 16:41 UTC
Read the original article Hit count: 378

Filed under:
|
|

I can't figure out how to get the same result from my Javscript as I do from my PHP. In particular, Javascript always leaves out the backslashes. Please ignore the random forward and backslashes; I put them there so that I can cover my basis on a windows system or any other system. Output:

Input String: "/root\wp-cont  ent\@*%'i@$@%$&^(@#@''mage6.jpg:"    
/root\wp-content\image6.jpg (PHP Output)
/rootwp-contentimage6.jpg (Javscript Output)

I would appreciate any help!

PHP:
<?php
$path ="/root\wp-cont  ent\@*%'i@$@%$&^(@#@''mage6.jpg:";
 $path = preg_replace("/[^a-zA-Z0-9\\\\\/\.-]/", "", $path);
 echo $path;
?>

Javascript:
<script type="text/javascript">
var path = "/root\wp-cont  ent\@*%'i@$@%$&^(@#@''mage6.jpg:"; //exact same string as PHP
var regx = /[^a-zA-Z0-9\.\/-]/g;
path = path.replace(regx,"");
document.write("<br>"+path);
</script>

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about php