Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of javascript regular expression and string

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly talks about "javascript regular expression and string example analysis". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "javascript regular expression and string example analysis".

How to create a string

String is one of the basic types in javascript, and its corresponding type is String. You can create a string in two ways:

Create the basic type of a string by assigning variables

Create a string object through the constructor (String)

Although the representation of the string created by the two ways is different, in some scenarios, what we need is a string, but we don't care whether it is a string primitive type or a string object. In this scenario, there is a slight change in the judgment of the string.

For more information, please refer to the following code:

Var s = 'abcd1234DCBA'; / / the recommended way to create a string var S1 =' abcd1234DCBA';var S2 = new String (s); / / create the string var S3 = new String (s); console.log (s===s1); / / true has the property of value type console.log (s===s2); / / false primitive type and object are not equal console.log (s2===s3); / / false different objects are not equal console.log (typeof s) / / stringconsole.log (typeof S2); / / object// determines whether the input value is a basic type string function isString (s) {return typeof s = 'string'} console.log (isString (s)); / / trueconsole.log (isString (S2)); / / false// determines whether the input value is a string (basic type + string object form) function isString2 (s) {return s! = null & & typeof s.valueOf () = =' string' } console.log (isString2 (s)); / / trueconsole.log (isString2 (S2)); / / true

The value invariant property of a string

Strings are immutable in Javascript. The so-called immutable means that when you generate a string, and then call the API of the string to operate the string, the original value of the string will not change, the result of the call is a new string.

You can refer to the following code to deepen your understanding:

Var s = new String ('abc'); var r = s.toUpperCase (); alert (s); / / abc s itself is alert (r) that will not change; / / ABC

Commonly used API-string interception

Not about the interception of strings, just master the following three API:

Slice: String.slice (N1 Jing N2) this is the string that we usually use from the specified position (N1) to the specified position (N2).

Substring: String.substring (N1 Jing N2) this is the string that we usually use from the specified position (N1) to the specified position (N2).

Substr: String.substr (N1 and N2) this is a string that we often use to intercept a specified length (N2) from a specified location (N1).

You can refer to the following code to deepen your understanding:

Var s = '0123456789'

Var R1 = s.substring (1); / / 123456789 the second parameter defaults to the character length

Var R2 = s.substring (1Magol 5); / / 1234

Var R3 = s.substring (1); / / 123456789 the second parameter defaults to the character length

Var R4 = s.substring (1Magol 5); / / 1234

Var R5 = s.substr (1); / / 123456789 the second parameter defaults to the character length

Var R6 = s.substr (2Magol 5); / / 23456

Var R7 = s.substr (2100); / / 23456789 the second parameter is larger than the character length and is not affected

Commonly used API-string retrieval

For string retrieval operations, you can master the following three API. String retrieval is common in other programming languages, and indexOf and lastIndexOf, the two API sub-languages, are very common and relatively easy to understand. About search, it's similar to indexOf, except that its input parameter is not a retrieved string, but a regular expression, and the return value is the index of the first match of the regular expression.

For more information, please refer to the following code for further understanding:

Var s = '0123abc401234 console console.log (s.indexOf (' 23')); / / 2console.log (s.lastIndexOf ('23')); / / 10console.log (s.search (/ [amurz] + / g)); / / 4 retrieve the starting position of the character where it appears

Commonly used API-string substitution

The replace method is generally called in JS to replace some characters in the string, which takes two parameters:

* the first parameter is used to describe the substring to be replaced, and the parameter type can be a string or a regular expression. It is important to note that if it is a string, only the first matching substring in the original string will be replaced, and if the regular expression identifying'g'is not set, the result will be the same. If you want to replace all matching substrings, you must pass in the regular expression with the identifier g

* the second parameter is used to describe the replacement value. The parameter type can be a string, a function, or it can contain special sequence characters (the static property of RegExp: − / & /'/'/ 1.. nUnion $, etc.).

For more information, please refer to the following code:

Code one. Basic matching operation

Var s = 'cat,bat,sat,fat';var res = s.replace (' at','NE'); console.log (res); / / cNE,bat,sat,fat replaces only the first match var res1 = s.replace (/ at/,'NE'); console.log (res1); / / cNE,bat,sat,fat or only the first match var res2 = s.replace (/ at/g,'NE'); console.log (res2) / / cNE,bNE,sNE,fNE replaces all matches

Code two. The second argument is the function

Var s = 'abcd';// simulated HTML match

< >

Escape var res = s.replace (/ [] / gMagna function (match,index,souStr) {switch (match) {case': return'& gt';}}); console.log (res); / / ab<name>cd

Code three. Only then can the special sequence characters be replaced flexibly.

/ / for the character sou, expand the keyword key with {} function strong (sou,key) {var re = new RegExp ('('+ key+')','g'); return sou.replace (re,' {$1}'); / / $1 first capture group} console.log (strong (srecoverat')) / / c {at}, b {at}, s {at}, f {at} / / for the character sou, expand the keyword key with {} function strong2 (sou,key) {var re = new RegExp (key,'g'); return sou.replace (re,' {$&}'); / / $& matching string} console.log (strong2); / c {at}, b {at}, s {at}, f {at}

Commonly used API-string grouping

The split method is used in JS to group strings, which receives two parameters:

* the first parameter represents the delimiter, which can be a string type or a RegExp object.

* the second parameter is optional, indicating the number of receiving groups, that is, the size of the returned array of results. If you do not specify this parameter, all groups are returned.

For more information, please refer to the following code:

Var s = 'cat,bat,sat,fat';var res = s.split (','); console.log (res); / / ['cat',' bat', 'sat',' fat'] var res2 = s.split (/, /); console.log (res2); / / ['cat',' bat', 'sat',' fat'] var res3 = s.split (/, /, 2); console.log (res3) / / ['cat',' bat'] only returns 2 groups

Commonly used API-string matching

The match () method retrieves the string stringObject to find one or more text that matches the regexp. The behavior of this method depends to a large extent on whether regexp has the flag g.

If regexp does not have a flag g, then the match () method can only perform a match once in the stringObject. If no matching text is found, match () returns null. Otherwise, it returns an array of information about the matching text it finds. The 0th element of the array holds matching text, while the rest of the elements hold text that matches the child expression of the regular expression. In addition to these regular array elements, the returned array contains two object properties. The index attribute declares the position of the starting character of the matching text in the stringObject, and the input attribute declares a reference to the stringObject.

If regexp has the flag g, the match () method performs a global search to find all matching substrings in the stringObject. If no matching substring is found, null is returned. If one or more matching substrings are found, an array is returned. However, the contents of the array returned by the global match are quite different from the former, in which all the matching substrings in stringObject are stored in the array elements, and there are no index or input attributes.

Note: in global retrieval mode, match () does not provide information about the text that matches the subexpression, nor does it declare the position of each matching substring. If you need this globally retrieved information, you can use RegExp.exec ().

For more information, please refer to the following code:

Var s = 'cat,bat,sat,fat'; var reg = / [Amurz] (at) /; console.log (s.match (reg)); / / [' cat', 'at', index: 0, input:' cat,bat,sat,fat'] var res = s.match (/ [Amurz] (at) / g); console.log (res); / / [cat', 'bat',' sat', 'fat']

Commonly used API-string comparison

There are two ways of comparison, one is to have a greater than or less than symbol, the other is to use the localeCompare method, note that this method returns a number, the meaning of the number is similar to that of other programming languages, and it is not explained too much. The areas involved are relevant, and it is recommended that you use localeCompare to compare strings.

For more information, please refer to the following code:

Var S1 = 'abc';var S2 =' bcd';var S3 = new String ('abc'); console.log (S1 > S2); / / trueconsole.log (s1==s3); / / true compares S1 with s3.toString () console.log (s1.localeCompare (S2)); / /-S1 is less than s2console.log (s1.localeCompare (S3)) The value of / / 0s1 is equal to S3. I believe you have a deeper understanding of "javascript regular expression and string example analysis". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report