Unicode matching Special characters _ regular expressions for regular expressions

Source: Internet
Author: User
Tags modifier numeric value

First of all, it is stated that all the code in this article is run under ES6, ES5 needs to be modified before it can be run, but this article does not involve too many ES6 new features, and because V8 to the U modifier is not supported, the final implementation is basically the code written with ES5 knowledge.

At first I just wanted to record that regular expressions Match Special characters in Unicode. Written to find that V8 not support the U modifier, and instead to study how to convert the string to utf-16 format, in the study of how to convert the ES5 of the regular pair of Unicode coding unit > The 0x10000 string is not supported, and then the conversion to a string that is greater than 0x10000 is implemented, and is hereby recorded.

Previously encountered a practical regular expression to match the needs of special characters, such as a text ' ab*cd$ hello I'm good]\nseg$me*ntfault\nhello,world ', the user can choose to use * or $ to split the string.

In JavaScript, $ and * are predefined special characters that cannot be written directly in regular expressions and need to be escaped, written as/\$/or/\*/.

We need to write regular expressions based on the user's choice, encapsulated as a function:

Copy Code code as follows:

function reg (Input) {
return new RegExp (' \\${input} ')
}

The writing looks beautiful at first, after the characters are escaped, some special characters can be matched, but the reality is brutal: when the user enters a character such as N or T, the returned regular expression is/\n/or/\t/, which matches all the tabs, which violates the user's original intention.

There is usually a writing that lists all the special characters that need to be escaped, and then matches each one, which is very energy-intensive and may result in a leak match because there are no special characters to count.

Unicode is a grand debut at this time, and in JavaScript we can also use Unicode to denote a character, such as ' A ' can be written ' \u{61} ', ' You ' can also be written ' \u{4f60} '.

About Unicode you can see Unicode and JavaScript in detail

The charCodeAt () method is provided in ES5 to return the Unicode value of the character at the specified index, except for the Unicode encoding unit > 0x10000, and a new method Codepointat () is added to the ES2015 to return greater than 0 The numeric value of the x10000 string. The value returned is decimal, and we also need to go through toString (16) to the 16 binary.

The following functions are encapsulated

Copy Code code as follows:

function Tounicode (s) {
Return ' \\u{${s.codepointat (). toString (16)}} '
}
Tounicode (' $ ')-> ' \u{24} '

Re-encapsulate the Reg function as

Copy Code code as follows:

function reg (Input) {
return new RegExp (' ${tounicode (Input)} ', ' U ')
}

In fact, I hope it's right, but unfortunately, V8 does not support the REGEXP u modifier. V8 support, write here should be over, it doesn't matter, here just provides a way to use Unicode to escape the idea of special characters.

Although V8 does not support the U modifier, as a pursuit of the code farmers, of course, can not stop here, we may also use other methods to continue to make this perfect

function Tounicode (s) {
 var a = ' \\u${utf (s.charcodeat (0). toString ()} '
 if (s.charcodeat (1))
 a = ' ${a}\\ U${utf (S.charcodeat (1). toString)} ' return 
 a  
}
function utf (s) {return
 array.from (') '. Concat (Array.from (s)). Slice ( -4). Join (')
}
//Here is VAR without the let declaration because the code is copied directly to the chrome console to see the execution results
//test
//Tounicode (' a ')  --> "\u0061"
//Tounitcode (")-->" \ud842\udfb7 "
function reg (Input) {
 return new RegExp (' ${tounicode (Input)} ')
}
//Test
Reg (' $ '). Test (' $ ')--> true

The above content is the cloud Habitat Community small make up to share the regular expression of the Unicode matching special characters

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.