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
Change the regex to
/[^s$]*$[^$]+$[^s$]*/gi
.That matches what you want it to match according to regex101:
You’d want something like:
[^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 charactershttps://regex101.com/r/ws0mn0/1
Probably you need smth like this
/S*?$$?[^$]*$$?S*/gi
That would grab all text inside dollar signs and surrounding text, which is not whitespace.