Home > Web Front-end > JS Tutorial > Usage of $ and ^ in Javascript regular expressions

Usage of $ and ^ in Javascript regular expressions

零到壹度
Release: 2018-03-27 13:45:27
Original
4316 people have browsed it

This article mainly shares with you the usage of $ and ^ in regular expressions in Javascript. I hope it can help you.

This time, let’s take a look at the usage of $ and ^ in regular expressions;

We now have the string str="abc. css"

Then we want to match .css , that is, verify that this is a css file

<span style="font-family: 微软雅黑, "Microsoft YaHei";"><script type="text/javascript"><br/>             var str="abc.css";<br/>             var pattern=/\.css$/;<br/>             console.log(str.match(pattern));<br/></script><br/></span>
Copy after login

Let’s see the result:

Array(1) 
                    0: ".css"
                    groups: undefined
                     index: 3
                     input: "abc.css"
                     length: 1__proto__: Array(0)
Copy after login

Okay, the result is an array, and it matches .css ;

Let’s take a look at /\.css$/ First of all, the meaning of \. It is to escape this dot. As we said before, dot can match any single character,

, so writing a dot directly will not work and needs to be escaped;

Finally we saw $, $ means matching from the right, that is, the end; the last digit of the string str is s, and the last digit of our .css

It matches, the penultimate digit of the string str is s, and it matches the penultimate digit of our .css...

Suppose our string is str ="abc.cssa", what is the result? Of course it is null, because the last digit does not match,

So, $ means to match from the end of the string from back to front;

I spent a long time looking at this $ , so the following ^ is very simple. These two are relative,

^ means from left to left Right matching, matching starts from the beginning of the string; I won’t go into details;

Note: If ^ appears in [ ], it means non It no longer means matching from the beginning,

, so pay special attention to it.

Related recommendations:

The ^/$ and i, m, g in regular expressions Some thoughts on using the "^" symbol in regular expressions

The meaning of symbols such as regular expression "^+$"

The above is the detailed content of Usage of $ and ^ in Javascript regular expressions. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template