admin管理员组文章数量:1355617
My code receives a RegExp object (out of my control). It isn't global but I need it to be.
At the moment I'm doing this:
if (!regex.global) {
var flags = 'g';
if (regex.ignoreCase) flags += 'i';
if (regex.multiline ) flags += 'm';
if (regex.sticky ) flags += 'y';
regex = new RegExp(regex.source, flags);
}
...because I can't figure out any other way.
regex.global
doesn't have a setter.regexpile(new_pattern)
is deprecated in favour ofnew RegExp(new_pattern)
regex.flags
isn't a thing
Is there a better way to do this?
My code receives a RegExp object (out of my control). It isn't global but I need it to be.
At the moment I'm doing this:
if (!regex.global) {
var flags = 'g';
if (regex.ignoreCase) flags += 'i';
if (regex.multiline ) flags += 'm';
if (regex.sticky ) flags += 'y';
regex = new RegExp(regex.source, flags);
}
...because I can't figure out any other way.
regex.global
doesn't have a setter.regex.pile(new_pattern)
is deprecated in favour ofnew RegExp(new_pattern)
regex.flags
isn't a thing
Is there a better way to do this?
Share Improve this question edited Jul 26, 2016 at 5:47 Rajeshwar 2,5274 gold badges34 silver badges49 bronze badges asked May 7, 2015 at 11:56 Julian TFJulian TF 4555 silver badges10 bronze badges 4- 4 Given that the internal structure of a regex depends, for example, on whether it's global or not (a global regex is an iterator), such a setter would be costly IMO (and what should it do ? Reset the state ?). Your solution is probably the best one. – Denys Séguret Commented May 7, 2015 at 12:01
- I'm going to risk it and ask - why do you need it to be global? – Benjamin Gruenbaum Commented May 7, 2015 at 12:12
-
1
@dystroy: you actually just saved me a future bug. Ie, if the regex IS global then I have to remember to reset regex.lastIndex to 0. So far as I can tell, state for a non-g regex and where sticky isn't implemented (mostly still the case) doesn't exist -> eg:
var r = /a/; var s = "ababa"; r.exec(s); r.exec(s); // r.lastIndex always stays on 0
– Julian TF Commented May 7, 2015 at 12:23 - @BenjaminGruenbaum: it's part of a 'matchAll', returning all hits in the target string for that regex. I can do it without 'g' but that means maintaining the state myself (eg, repeatedly slicing the string after each match as there appears to be no way to tell the regex where to start from on each new non-global match). – Julian TF Commented May 7, 2015 at 12:25
3 Answers
Reset to default 8You can now do this in ES6. By "this" I don't mean modify the flags on an existing regexp, but rather create a new regexp from an existing one which replaces or modifies its flags. See http://www.2ality./2015/07/regexp-es6.html. Example from that post:
function copyWithIgnoreCase(regex) {
return new RegExp(regex, regex.flags + 'i');
}
I'm afraid the answer is "No".
There is no way to change the flags on a existing regex. What you're doing seems to be the cleanest way.
You (arguably) could shorten the optional flags a bit:
var flags = 'g' +
(regex.ignoreCase ? 'i' : '') +
(regex.multiline ? 'm' : '') +
(regex.sticky ? 'y' : '');
Even though you can't change the flags of an existing RegExp
you can create a new one with modified flags. One nice option is using YourJS.flagRegExp()
:
rgx1 = /[A-Z]/g;
rgx2 = YourJS.flagRegExp(rgx1, 'i');
// > /[A-Z]/gi
rgx3 = YourJS.flagRegExp(rgx1, '!i!g');
// > /[A-Z]/i
The API documentation is found here. You will be able to customize your own JS library with that any other functions you want for your project.
本文标签: javascriptIs it possible to modify flags on an existing RegExpStack Overflow
版权声明:本文标题:javascript - Is it possible to modify flags on an existing RegExp? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743950397a2567184.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论