Get the App
SLTechnology News&Howtos  ›  Network Security  › 

Reference type-RegExp type

Shulou Source: shulou.com Published: 2022-06-01 04:29:32 10月05日 Update

JavaScript Advanced Programming (3rd Edition) Notes-Chapter 5-RegExp Types

There are two ways to define regular expressions

1. Define regular expressions in literal form:

ver expression = /patterns/flags;

patterns: can be any simple or complex regular expression

flags:

g: global

i: means case insensitive

m: indicates multi-row mode, which continues to find the next row when it reaches the end of one row

var patterns1 = /at/g;//matches all instances of "at" in characters var patterns2 = /[bc]at/i;//matches all "bat" or "cat", case insensitive var patterns3 = /.at/gi;//matches all combinations of three characters ending in "at", case insensitive

All metacharacters used in patterns must be escaped, metacharacters in regular expressions include: ( [ { \ ^ $| ) ? * + . ] }

var patterns4 = /\[bc\]at/i;//matches the first "[bc]at", case insensitive

2. Define a regular expression with RegExp constructor, which accepts two parameters (patterns,flags)

var patterns2 = new RegExp("[bc]at","i");//Same as var patterns2 = /[bc]at/i;

Also use RegExp to escape metacharacters and double escape

vqr patterns 4 = new RegExp("\\[bc\\]at","i"); Same as var patterns4 = /\[bc\]at/i;

These two methods create different regular expressions. Regular expression literals always share a RegExp instance, and each new RegExp instance created using a constructor is a new instance

var re = null, i; for(i=0;i

Tags: Character expression regular string instance property function method flag pattern parameter size metacharacter global escape same one line two position information Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Redmi Linux Apple MariaDB macOS