- xml - AJAX/Jquery XML 解析
- 具有多重继承的 XML 模式
- .net - 枚举序列化 Json 与 XML
- XML 简单类型、简单内容、复杂类型、复杂内容
MDN 为 JavaScript RegExp 引入了“y”粘性标志。这是一个documentation excerpt :
y
sticky; matches only from the index indicated by the lastIndex property of this regular expression in the target string (and does not attempt to match from any later indexes).
还有一个例子:
var text = 'First line\nSecond line';
var regex = /(\S+) line\n?/y;
var match = regex.exec(text);
console.log(match[1]); // prints 'First'
console.log(regex.lastIndex); // prints '11'
var match2 = regex.exec(text);
console.log(match2[1]); // prints 'Second'
console.log(regex.lastIndex); // prints '22'
var match3 = regex.exec(text);
console.log(match3 === null); // prints 'true'
但在这种情况下,g 全局标志的用法实际上没有任何区别:
var text = 'First line\nSecond line';
var regex = /(\S+) line\n?/g;
var match = regex.exec(text);
console.log(match[1]); // prints 'First'
console.log(regex.lastIndex); // prints '11'
var match2 = regex.exec(text);
console.log(match2[1]); // prints 'Second'
console.log(regex.lastIndex); // prints '22'
var match3 = regex.exec(text);
console.log(match3 === null); // prints 'true'
相同的输出。所以我想可能还有其他关于 'y' 标志的东西,而且 MDN 的示例似乎不是这个修饰符的真正用例,因为它似乎只是在这里作为 'g' 全局修饰符的替代品。
那么,这个实验性“y”粘性标志的真实用例可能是什么? “仅从 RegExp.lastIndex 属性匹配”的目的是什么?当与 RegExp.prototype.exec
一起使用时,它与“g”的区别是什么?
感谢关注。
最佳答案
y
和 g
之间的区别在 Practical Modern JavaScript 中有描述。 :
The sticky flag advances
lastIndex
likeg
but only if a match is found starting atlastIndex
, there is no forward search. The sticky flag was added to improve the performance of writing lexical analyzers using JavaScript...
至于一个真实的用例,
It could be used to require a regular expression match starting at position
n
wheren
is whatlastIndex
is set to. In the case of a non-multiline regular expression, alastIndex
value of0
with the sticky flag would be in effect the same as starting the regular expression with^
which requires the match to start at the beginning of the text searched.
这是该博客中的一个示例,其中 lastIndex
属性在 test
方法调用之前被操作,从而强制产生不同的匹配结果:
var searchStrings, stickyRegexp;
stickyRegexp = /foo/y;
searchStrings = [
"foo",
" foo",
" foo",
];
searchStrings.forEach(function(text, index) {
stickyRegexp.lastIndex = 1;
console.log("found a match at", index, ":", stickyRegexp.test(text));
});
结果:
"found a match at" 0 ":" false
"found a match at" 1 ":" true
"found a match at" 2 ":" false
关于javascript - JavaScript 正则表达式中 'y' 粘性模式修饰符的用途是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30291436/
XML可以应用于 WEB 开发的许多方面,常用于简化数据的存储和共享 XML 把数据从 HTML 分离 现在的HTML 文档一般都是显示动态数据,每当数据改变时将花费大量的时间来编辑 HTML
在类(class)(方案)中,我们最近学习了允许函数接受任意数量参数的语法,例如; (define len (lambda args (length args))) >(len 1 2 3
在 redis-cli 中执行 info 命令时,会列出与该服务器相关的所有信息。 “evicted_keys”的目的是什么? 最佳答案 Redis 可以配置为根据需要自动清除键。如果这样配置,red
golang 空结构体 struct{} 可以用来节省内存 ? 1
有人可以解释一下资源文件夹中的 Info.plist 是什么吗?我如何在我的应用程序中使用它?提前致谢。 最佳答案 您可以在Info.plist文件中为所有类型的设备指定不同的常规资源和应用程序配置,
我需要减小可执行文件的大小,并且经常在我的可执行文件中将某些“不必要的” uses占用一定的大小。 例如: Unit1连接到Unit2,但是两个单元的导入为“ System.SysUtils”。 然后
我是 OOP 的新手。虽然我了解多态是什么,但我无法真正使用它。我可以有不同名称的函数。为什么我应该尝试在我的应用程序中实现多态性。 最佳答案 在严格类型化的语言中,多态性对于拥有不同类型对象的列表/
Possible Duplicate: What is the difference between a delegate and events? Possible Duplicate: Differ
这个问题在这里已经有了答案: Custom index.html javadoc page? (2 个答案) 关闭 7 年前。 我使用的是eclipse。一般情况下我们可以使用javadoc.exe
你能解释一下吗ValidateAntiForgeryToken目的并向我展示有关 MVC 4 中的 ValidateAntiForgeryToken 的示例? 我找不到任何解释此属性的示例? 最佳答案
我正在学习 RxSwift 并试图了解 Observable 和 PublishSubject aka Sequence 之间的主要区别。 据我了解,Sequences/PublishesSubjec
有时有人说,在使用 Azure 表时,实际上存在第三个关键分区数据 - 表名称本身。 我在执行分段查询时注意到 TableContinuationToken 有一个 NextTableName 属性。
在Apple提供的aurioTouch项目中,听说在performRender()函数下的AudioController文件中的AudioBufferList ioData携带了mic到音频播放器的音
MSDN 文档没有详细解释它的用途。 由于它是可选的,我几乎假设它类似于某种元数据。 我现在如何看待它,我想用它来为特定用户指定缓存,例如: new CacheItem(key: "keyName",
我正在学习 browserify,我正在尝试用它做两件基本的事情: 转换(通过 shim)非 CommonJS 模块以实现易用性和依赖跟踪 捆绑项目特定的库 我找到了一个工作流程,说明如何使用 Gul
我通过按 CTRL + C 访问 eix 1.8.2 的 Break 菜单。它看起来像这样: BREAK: (a)bort (c)ontinue (p)roc info (i)nfo (l)oaded
在 codeLabs 教程(Android - Kotlin - Room with a View)中,他们使用“viewModelScope.launch(Dispatchers.IO)”来调用插入
我通过按 CTRL + C 访问 eix 1.8.2 的 Break 菜单。它看起来像这样: BREAK: (a)bort (c)ontinue (p)roc info (i)nfo (l)oaded
请耐心等待,因为我没有太多地使用压缩算法,所以这对你们中的一些人来说可能是显而易见的。当某些流媒体视频开始滞后时,我总是注意到这一点。当我看到这个问题时我才意识到我很好奇: Twitter image
关闭。这个问题是opinion-based 。目前不接受答案。 想要改进这个问题吗?更新问题,以便 editing this post 可以用事实和引文来回答它。 . 已关闭 3 年前。 Improv
我是一名优秀的程序员,十分优秀!