Page MenuHomePhabricator

"replace all" does not work with a regular expression that contains a caret or a dollar
Closed, ResolvedPublic

Description

Hello,

The algorithm for "replace all" in WikiEditor, as described in a comment in modules/jquery.wikiEditor.dialogs.config.js, is the following: "Instead of using repetitive .match() calls, we use one .match() call with /g and indexOf() followed by substr() to find the offsets."

In general, this does not work when regular expression mode is turned on and the regular expression contains "^" or "$", because calling indexOf on the matched text ignores the constraint that this text must be at the beginning/end of a line.

Example:

  • Open a page in edit mode and set the text of the edit box to "aa"
  • Open the search and replace dialog of WikiEditor.
  • Set "search for" to "a$"
  • Set "replace with" to "b"
  • Enable "Treat search string as a regular expression"
  • Click "Replace all"

Expected result: "ab"
Real result: "ba"
What happens: the content of the only match is "a", and the first occurrence of "a" in "aa" is at position 0.


Version: unspecified
Severity: normal

Event Timeline

bzimport raised the priority of this task from to Medium.Nov 22 2014, 2:40 AM
bzimport added a project: WikiEditor.
bzimport set Reference to bz56228.
bzimport added a subscriber: Unknown Object (MLST).

Thanks for taking the time to report this and the great analysis!

Would you interested in providing a patch to fix the problem? See https://www.mediawiki.org/wiki/Developer_access and https://www.mediawiki.org/wiki/Gerrit/Tutorialfor more information.

Sorry, for personal reasons, I don't want to contribute directly to MediaWiki source code at the moment.

Change 474483 had a related patch set uploaded (by TheDJ; owner: TheDJ):
[mediawiki/extensions/WikiEditor@master] Search & Replace: Use actual global replace

https://gerrit.wikimedia.org/r/474483

Change 474483 merged by jenkins-bot:
[mediawiki/extensions/WikiEditor@master] Search & Replace: Use actual global replace

https://gerrit.wikimedia.org/r/474483

TheDJ claimed this task.
TheDJ removed a project: Patch-For-Review.