skip to Main Content

I have this string:
'Slope fields are constructed by plotting tiny line segments at various points in the $xy$-plane. The slope of each line segment is given by the value of $f(x, y)$ at that corresponding point. These line segments collectively form a field of slopes, hence the name "slope field."'

and also this regex: /S*$$?[^$]*$$?S*/gi

which i feel should match all phrases within single or double dollar signs $ as well as any text that surrounds it. so ideally it should math $xy$-plane. and $f(x, y)$

but for some reason it matches: $xy$-plane. The slope of each line segment is given by the value of $f(x,

anyone has any idea what im doing wrong or whats going on here?

I am expecting it to match $xy$-plane. and $f(x, y)$ but it doesn’t

3

Answers


  1. Change the regex to /[^s$]*$[^$]+$[^s$]*/gi.
    That matches what you want it to match according to regex101:
    enter image description here

    Login or Signup to reply.
  2. You’d want something like:

    [^s$]*$[^$]+$[^s$]*
    
    • [^s$]* – match zero or more non-whitespace and not dollar sign characters before the opening dollar sign
    • $ – opening dollar sign
    • [^$]+ – one or more not dollar signs
    • $ – closing dollar sign
    • [^s$]* – zero or more non-whitespace and not dollar sign characters

    https://regex101.com/r/ws0mn0/1

    const str = 'Slope fields are constructed by plotting tiny line segments at various points in the $xy$-plane. The slope of each line segment is given by the value of $f(x, y)$ at that corresponding run-on-sentence$()$followed-by-another$DHF$group point. These line segments collectively form a field of slopes, hence the name "slope field." and more some-$yz$-something else';
    
    const match = str.match(/[^s$]*$[^$]+$[^s$]*/g);
    
    console.log(match);
    Login or Signup to reply.
  3. Probably you need smth like this

    /S*?$$?[^$]*$$?S*/gi

    That would grab all text inside dollar signs and surrounding text, which is not whitespace.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search