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