Sticky flag y, searching at position

Sticky flag “y”, searching at position (置顶标志“y” ,搜索位置)

Let’s explore the process of search with the help of the y flag. It helps to implement the search at a particular position in the source string. (让我们借助y标志探索搜索的过程。它有助于在源字符串中的特定位置实现搜索。)

For a better understanding of what it is, let’s start from a practical case. One of the most frequent tasks of the regular expressions is “lexical analysis”. For example, HTML includes attributes and tags, JavaScript contains variables, functions, and more. (为了更好地理解它是什么,让我们从一个实际案例开始。 正则表达式最常见的任务之一是“词汇分析”。 例如, HTML包含属性和标记, JavaScript包含变量、函数等。)

Lexical analyzers is a unique area with its algorithms and tools. The common task is reading something at a particular position. (词法分析仪是一个具有其算法和工具的独特领域。常见的任务是在特定位置阅读内容。)

Imagine having a string let let elName = “value” and intend to read the variable name from it, which begins at position 4. (想象一下,有一个字符串let elName = “value” ,并打算从中读取变量名称,该变量名称从位置4开始。)

The str.match(/\w+/) call will detect only the first word in the line, either all the words with g flag. But you need just a single word at position 4. (Str.match (/\ w +/)调用将仅检测行中的第一个单词,或者所有带有g标志的单词。但你只需要在第4位说一个字。)

To investigate from a particular position the regexp.exec(str) method is used. In case the regexp has no g or y flags, then this method will search for the first match in the string str just as str.match(regexp). (要从特定位置进行调查,请使用regexp.exec (str)方法。 如果正则表达式没有g或y标志,则此方法将像str.match (regexp)一样在字符串str中搜索第一个匹配项。)

In case there is g flag, it implements the search in the string str, beginning from the position, stored in the regexp.lastIndex property. If a match is detected, it sets regmatch. exp.lastIndex to the index right after the match. (如果有g标志,它会在字符串str中实现搜索,从存储在regexp.lastIndex属性中的位置开始。如果检测到匹配,则在匹配后立即将regmatch. exp.lastIndex设置为索引。)

The lastIndex of a regexp is 0 at the moment of its creation. Then a call to regexp.exec(str) returns matches one by one. (正则表达式的lastIndex在创建时为0。然后调用regexp.exec (str)返回一个接一个的匹配。)

Here is an example with the g flag:

let str = 'let elName';

let regexp = /\w+/g;
console.log(regexp.lastIndex); // 0 (lastIndex=0)

let str1 = regexp.exec(str);
console.log(str1[0]); // let (1st word)
console.log(regexp.lastIndex); // 3 (post match position)

let str2 = regexp.exec(str);
console.log(str2[0]); // elName (2nd word)
console.log(regexp.lastIndex); // 10 (post match position)

let str3 = regexp.exec(str);
console.log(str3); // null (no more matches)
console.log(regexp.lastIndex); // 0 (resets at the end of the search)

Each match is returned as an array with additional properties and groups. (每个匹配项都作为数组返回,其中包含其他属性和组。)

All the matches can be received in the loop, as follows:

let str = 'let elName';
let regexp = /\w+/g;
let results;
while (results = regexp.exec(str)) {
	console.log(`Found ${results[0]} at position ${results.index}`);
	// Found let at position 0
	// Found elName at position 4
}

So, this is an alternative to the str.matchAll method. (因此,这是str.matchAll方法的替代方案。)

You can set lastIndex for starting the search from a particular position. (您可以设置lastIndex从特定位置开始搜索。)

In the example below, a word, beginning from the position 4 is being found:

let str = 'let elName = "elValue"';
let regexp = /\w+/g; // without flag "g", property lastIndex is ignored
regexp.lastIndex = 4;
let word = regexp.exec(str);
console.log(word); // elName

Please, take into account that the search begins at lastIndex position, then going further. If there isn’t any word at the lastIndex position, but it’s after it, then it will be detected:

let str = 'let elName = "elValue"';
let regexp = /\w+/g;
regexp.lastIndex = 3;
let word = regexp.exec(str);
console.log(word[0]); //elName
console.log(word.index); // 4

With the g flag, the lastIndex property sets the beginning position of the search. (使用g标志, lastIndex属性设置搜索的开始位置。)

The y flag urges regexp.exec to search for at the lastIndex position, not after it, not before it. (Y标志敦促regexp.exec在lastIndex位置搜索,而不是在它之后,也不是在它之前。)

So, the search with the y flag will look like this:

let str = 'let elName = "elValue"';
let regexp = /\w+/y;
regexp.lastIndex = 3;
console.log(regexp.exec(str)); // null (there's a space at position 3, not a word)
regexp.lastIndex = 4;
console.log(regexp.exec(str)); // elName (word in position 4)

So, the /\w+/y regexp does not correspond at position 3. (因此,/\ w +/y正则表达式不对应于位置3。)

As a conclusion, we can state that the y flag is a useful means for doing a search, and can be considered as a key to a good implementation. (作为结论,我们可以说y标志是进行搜索的有用手段,并且可以被视为良好实现的关键。)



请遵守《互联网环境法规》文明发言,欢迎讨论问题
扫码反馈

扫一扫,反馈当前页面

咨询反馈
扫码关注
返回顶部