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 does javascript determine what a string begins with

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

Share

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

It is believed that many inexperienced people are at a loss about how javascript determines what a string begins with. Therefore, this article summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.

Judgment method: 1, use "string object .substr (0, compare characters) =" compare characters "statement; 2, use" string objects .slice (0, number of characters) = = "compare characters" statement; 3, use "string object .indexOf (" compare characters ") = = 0" statement; 4, use regular expressions.

The operating environment of this tutorial: windows7 system, javascript1.8.5 version, Dell G3 computer.

Javascript determines what the string begins with

Method 1:substr () method

If ("123" .substr (0,2) = =" 12 ") {console.log (true);}

Method 2:slice () method

If ("123" .slice (0jin2) = =" 12 ") {console.log (true);}

Method 3:indexOf () method

If ("123" .indexOf (" 12 ") = = 0) {console.log (true);}

Method 4: regularization

If ("123.search (" 12 ")! =-1) {console.log (true);} if (new RegExp (" ^ 12.match $"). Test (12)) {console.log (true);} if (" 12 ".match (new RegExp (" ^ 12.match $")) {console.log (true);} after reading the above, have you mastered how javascript can tell what a string begins with? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

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

Development

Wechat

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

12
Report