AS3 the substitution of a backslash in a regular expression

Source: Internet
Author: User

An interesting little question, can the following regular expression replace success?

var __str:string = \' 12346789\ '; Trace (__str.replace (/\ \/g, \' 5\ ' ));

The answer is: no . The result of the trace is:

[Trace] 12346789

In fact, the regular itself is not written wrong, wrong in the string being replaced. The backslash "" is an escape character in AS3, and any subsequent value will be converted to itself, so the string you see actually is, that is 12346789 , no backslash, of course, cannot be searched.

Directly trace(__str) , the result is the same as the trace above.

To get the correct result, you need to set the string to: 1234\\6789 we see two backslashes, and AS3 thinks it's a backslash.

If you are using regexp to establish a regular, you need to use 4 backslashes:

var __str:string = \' 1234\\6789\ '; var New REGEXP (\' \\\\\ ', \' \ '); Trace (__str.replace (__reg, \' 5\ '));

This happens only when a hard-coded string is present, and if the string appears in TextField, the string taken from Textfield.text is automatically escaped, and a backslash is actually two backslashes.

Transferred from: http://zengrong.net/post/1374.htm

AS3 the substitution of a backslash in a regular expression

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.