I am working a discrete math parser to turn equations into abstract syntax trees.
"!!(a)"
To do this, I must the string above into the following: ["!!(", "a", ")"]
If an exclamation point is before an open parenthesis or letters, then split at that point.
I created the following regex (only a part of it, excluding operators).
let equation = "!!(a)".split(/\!*\(|\)|\!*[a-z]+/gi);
If ( starts with zero or more !
OR
is a )
OR
is a letter starting with zero or more !
The issue is that this returns empty strings in an array, as it is splitting and removing the element rather than capturing it.
["", "", "", ""]
How can I tell split to split between these two elements rather than the elements themselves? Is their a better way to do this? If I can split like this, can it be modular to add operators?
Hi, String.split takes in an input, and uses it as a separator. This means if you do "I am a sentence.".split(" "); you get ["I", "am", "a", "sentence"] and the same thing happens when you use a regex instead of a string as parameter. Whenever Javascript finds a regex match, thats where Javascript splits the string.
What you are looking for, is String.match which can capture all regex matches in a string as long as you have the global flag.
Here is an example
I’m slightly unclear on what exactly you want here. One thing to note is if the characters within the [] is meant to replace the [a-z] of your original regex, then you shouldn’t enclose it in a negative lookahead.
One website I always recommend for figuring regex out in a fast and responsive manner, is https://regex101.com/ you can look on the right to see what the regex you wrote will do, and how that compares to what you think it should do. You can also write testcases and see the regex work in real time.
if you are doing an invalid character check, you can do a simple regex like /[Ω∅∪∩⊕]/gi for the invalid characters of “Ω∅∪∩⊕”. Then do a .test on the user input. If it returns true then you know the user input contains invalid characters.
For a valid character check, you can do something like /^[\w]*$/gi which would make sure the user input from beginning to end would only contain a-zA-Z0-9_