mathlive icon indicating copy to clipboard operation
mathlive copied to clipboard

Regex replace replaces entire field value instead of matched value

Open wb-joona opened this issue 3 years ago • 2 comments

Description

When using MathfieldElement.replace() with a regex as the matcher, the entire mathfield value gets replaced. For example, mf.replace("⁵", "^5") does replace the unicode superscript 5 with ^5, but mf.replace(/⁵/, "^5") replaces the entire field value with ^5.

Steps to Reproduce

  1. Open the demo at https://codesandbox.io/s/musing-feistel-0rk4n?file=/src/index.js
  2. Click on "replace with text" to execute mf.replace('⁵', '^5)
  3. Click inside the math field, observe that the unicode ⁵ has been replaced with ^5.
  4. Click on "reset".
  5. Click on "replace with regex" to execute mf.replace(/⁵/, '^5')
  6. Click inside the math field, observe that the entire field value has been replaced with ^5.

Actual Behavior

The entire math field value gets replaced when replacing with a regex.

Expected Behavior

I expected that only the part matched by the regex would be replaced with the regex.

Additionally, replace doesn't seem to update the visual field value until the element is focused. I'm not sure if this is intended or not.

Motivation

I'm trying to replace unicode superscripts and subscripts on input as some Linux distros seem to produce unicode superscripts when typing ^5 (although typing ^^5 does emit ^, then 5, as expected). It would be handy for me to able do a .replace() with /[\u00B2-\u00B3\u00B9\u2070-\u2071\u2074-\u208E\u2090-\u209C]/ to handle these characters with a replacer function.

Environment

MathLive version 0.69.5

Operating System Windows 10 Pro N 20H2 build 19042.1110

Browser

  • Chrome 92.0.4515.131 (Official Build) (64-bit)
  • Firefox 90.0.2 (64-bit)
  • Edge 92.0.902.67 (Official build) (64-bit)

wb-joona avatar Aug 10 '21 08:08 wb-joona

I think this is actually a duplicate of #1118 (or at least, same underlying cause). After fixing #1118 I can't reproduce this. I couldn't reproduce the issue with the field not updating after replace either. Let me know if you still see a problem.

arnog avatar Aug 13 '21 15:08 arnog

@arnog I'm able to reproduce this with the latest version and the following Regex: /\^(?!{.+}|\S)/g. What I want to achieve is to make sure that when the user deletes the superscript, the empty "placeholder" will be removed (cursor will go down a level):

| symbolizes the cursor caret

# User enters the the expression with a superscript
5^2|

# User deletes the superscript using one Backspace press
5^|

# Now when the user will try to type something, it will appear on the superscript level - I want it to be even with the number 5 in this case
5^+|
# ^ should be `5+|` instead

Unfortunately the replace function replaces the entire content. Here is the exact piece of code:

mf.replace(/\^(?!{.+}|\S)/g, '')

xxczaki avatar Jan 25 '22 22:01 xxczaki

This is no longer supported.

arnog avatar Dec 08 '23 22:12 arnog