javascript-php Regular Replacement problem

Source: Internet
Author: User

JS under the regular is this:

var str = "data\/ui_inner\/74\/";str.replace(/\\/,"");

PHP does not write as follows:

$auto_url = preg_replace('/\\/','', $str);

My goal is to replace \ Empty in the string, how can I replace it?

According to the answer downstairs to try or not.

Find the reason, is because Json_encode () caused, how to solve it? I'm going to Google.

Reply content:

JS under the regular is this:

var str = "data\/ui_inner\/74\/";str.replace(/\\/,"");

PHP does not write as follows:

$auto_url = preg_replace('/\\/','', $str);

My goal is to replace \ Empty in the string, how can I replace it?

According to the answer downstairs to try or not.

Find the reason, is because Json_encode () caused, how to solve it? I'm going to Google.

Unless the landlord is neat, it is not recommended to go to json_encode replace the results, because json_encode the / escape output, for JS, its content is / , and not \/ .

(PS: The following in the test when there are comments in the code var_dump , only reserved echo )
Replaced by:

Not replaced by:

1.htmlThe code is as follows:

PHP provides a function preg_quote

string preg_quote ( string $str [, string $delimiter = NULL ] )

Preg_quote () requires the parameter str and adds a backslash to the character in each of the regular expression syntax. This is typically used when you have some run-time strings that need to be matched as regular expressions.

The regular expression special characters are:. \ + * ? [ ^ ] $ ( ) { } = ! < > | : -


  
   

所以以上代码可以这样写

 

This should be the case

 $auto _url = preg_replace ('/\\\\/', ' ', $str);  
The

is also just learned, because in the string, ' \ ' is also escaped, '/\\/' becomes '/\/', so to four.
You can also use Str_replace

  $auto _url = str_replace (' \ \ ', ' ', $str);  

The single quotation mark in PHP will still escape \
So you need to do this

 $auto _url = preg_replace ('/\\\\/', ' '," data\/ui_inner\/74\/");  

to four \\\\

It is unsafe to do regular replacement of JSON directly, because if the business logic changes, You may have accidentally modified other domains, it is recommended to decode and then encode

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.