Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(js/string_util): drop redundant new RegExp() #103

Closed
wants to merge 1 commit into from

Conversation

IepIweidieng
Copy link
Contributor

@IepIweidieng IepIweidieng commented Dec 15, 2021

This resolves #77

Regex literals (codes like /pattern/flags) are already instances of RegExp, thus new RegExp(/pattern/flags) creates an extra copy of the RegExp object, which is often not intended.

This issue was introduced in iamchucky/PttChrome.

This issue is fixed by rewritten new RegExp() codes into plain regex literals.

@IepIweidieng IepIweidieng changed the title chore(js/string_util): drop redundant new RegExp() refactor(js/string_util): drop redundant new RegExp() Dec 15, 2021
This solves robertabcd#77

Regex literals (codes like `/pattern/flags`) are already instances of RegExp,
thus `new RegExp(/pattern/flags)` creates an extra copy of the RegExp object,
which is often not intended.

This issue was introduced in iamchucky/PttChrome.

This issue is fixed by rewritten `new RegExp()` codes into plain regex literals.
@IepIweidieng
Copy link
Contributor Author

IepIweidieng commented Dec 15, 2021

This fix is invalid. Closed.

The new RegExp() is not redundant due to regex.exec() is executed on the RegExp object named regex, so that the regex.lastIndex is advanced if regex has the g (global) flag.
However, because the RegExp object of regex literal is only created at the time the script is loaded, if new RegExp() is dropped, the variable regex will always point to the same object, and therefore regex.lastIndex will not be reset.

The new RegExp() will ensure that regex points to a newly created RegExp object each time when the function is executed, thus avoiding the non-resetting regex.lastIndex problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

解析水球的正則表示式的呈現方式問題
1 participant