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

How to use regular expression to realize string lookup in JavaScript

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

Share

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

How to use regular expressions to achieve string lookup in JavaScript? In view of this problem, this article introduces the corresponding analysis and answers in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible way.

First of all, a question is raised:

How do I get the number of times a given string substr appears in another string str?

String matching, the first thing that comes to mind is regular expressions, but the regular expressions we use most often to create literals cannot pass in variables.

You should use another way to create a regular expression: the constructor, as follows

Var reg = new RegExp (substr, "g")

The first parameter represents the string pattern to match, so you can pass in a variable without adding / /, and the second parameter is an optional flag string.

You can pass in variables, and then introduce a method of string basic wrapper type: match ()

The syntax is str.match (regExp), and the parameter is a regular expression. If it is not regular, it will be implicitly converted. The return value will be an array containing the matching results. If there is no match, null will be returned.

In addition, the match method of the string is similar to the regular exec (), returning the details of the match; the search method of the string is similar to the regular test (), except to see if there is a match.

Going back to the original question, the complete procedure is as follows:

Var str1 = "abctestctesqk1test23"; var str2 = "test"; function countSubstr (str, substr) {var reg = new RegExp (substr, "g"); return str.match (reg)? Str.match (reg) .length: 0 null / if match returns not null, the result is true. Output the length of the array returned by match (["test", "test"])} console.log (countSubstr (str1, str2)); / / output 2

In addition, the problem of variables can be solved without using a constructor, that is, using eval ():

Var reg = "/" + substr + "/ g"; reg = eval (reg); / / not recommended!

But we all know that the eval () method is not recommended, so the constructor method is recommended.

But there is still a problem, if the substring contains the so-called metacharacters in regular expressions (that is, + *? (^, etc.), it does not match properly.

Because the regular expression is in the string at this time,\ is the escape character in the string, and it is also the escape character in the regular expression. If you only add a\, you can only escape in a string, while js needs to further change the\ in the ordinary string into the\ in the regular expression, like a deeper meaning of transformation, called double escape, so that\\ means the escape character (\) in the regular expression.

So for those metacharacters without double escape, you can't really find the character you're looking for. This problem has not been solved, but the general string search also rarely have these special characters, you can use it first.

This is the answer to the question about how to use regular expressions to find strings in JavaScript. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel for more related knowledge.

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