Home Web Front-end JS Tutorial Analysis of multiline(/m) usage in JS regular expression modifier

Analysis of multiline(/m) usage in JS regular expression modifier

Jan 09, 2017 pm 03:36 PM

The example of this article analyzes the usage of multiline(/m) in JS regular expression modifier. Share it with everyone for your reference, the details are as follows:

JavaScript regular expressions have three modifiers /i, /m and /g. /i is the most commonly used and best understood one, which means that regular expressions are not case-sensitive when matching.

var regex = /abc/i;
alert(regex.test("aBc"));//true
Copy after login

/m represents multiline mode. If the target string does not contain a newline character \n, that is, there is only one line, then the /m modifier does not have any significance.

var multiline = /abc/m;
var singleline = /abc/;
//目标字符串不含换行符\n
var target = "abcabcabc";
Copy after login

If the regular expression does not contain ^ or $ to match the beginning or end of the string, then the /m modifier has no meaning.

//正则表达式不含^或$
var multiline = /abc/m;
var singleline = /abc/;
var target = "abcab\ncabc";
Copy after login

That is to say, the /m modifier only works when the target string contains \n and the regular expression contains ^ or $. . If multiline is false, "^" matches the beginning of the string and "$" matches the end of the string. If multiline is true, "^" matches the beginning of the string and after "\n" or "\r", and "$" matches the end of the string and before "\n" or "\r" position matches.

var mutiline = /^abc/m;
var singleline = /^abc/;
var target = "ef\r\nabcd";
alert(mutiline.test(target));//true
alert(singleline.test(target));//false
Copy after login

\r\n represents a newline under Windows. If there is only one \n, it has the same effect. Since the target is not a string starting with abc, the result of matching singleline is false; because the target is a multi-line string (containing \n), and the second line starts with abc, the result of matching multiline is true.

I hope this article will be helpful to everyone in JavaScript programming.

For more articles related to the usage analysis of multiline(/m) in JS regular expression modifiers, please pay attention to the PHP Chinese website!


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

Hot Article

Hot Article

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Replace String Characters in JavaScript Replace String Characters in JavaScript Mar 11, 2025 am 12:07 AM

Replace String Characters in JavaScript

jQuery Check if Date is Valid jQuery Check if Date is Valid Mar 01, 2025 am 08:51 AM

jQuery Check if Date is Valid

jQuery get element padding/margin jQuery get element padding/margin Mar 01, 2025 am 08:53 AM

jQuery get element padding/margin

Top 5 Date Manipulation JS Plugins Top 5 Date Manipulation JS Plugins Feb 28, 2025 am 12:34 AM

Top 5 Date Manipulation JS Plugins

10 Worth Checking Out jQuery Plugins 10 Worth Checking Out jQuery Plugins Mar 01, 2025 am 01:29 AM

10 Worth Checking Out jQuery Plugins

10 jQuery Accordions Tabs 10 jQuery Accordions Tabs Mar 01, 2025 am 01:34 AM

10 jQuery Accordions Tabs

jquery add scrollbar to div jquery add scrollbar to div Mar 01, 2025 am 01:30 AM

jquery add scrollbar to div

Custom Google Search API Setup Tutorial Custom Google Search API Setup Tutorial Mar 04, 2025 am 01:06 AM

Custom Google Search API Setup Tutorial

See all articles