Reputation: 9407
I have the following text that I need to split...
'(!false =>stuff <300^ OR <=200 "TEST DATA")'
There are a couple of rules. I need to preserve quoted texts. Also, the delimiters I need to split on are the following...
{'<', '>', '<=', '=>', '=', '!', '(', ')'}
In this case, my split is the following...
['(', '!', 'false', '=>', 'stuff', '<', '300^', 'OR', '<=', '200', '"TEST DATA"', ')']
I've gotten close...
input_text.match(/"[^"]*"|=[<>]|[<>]=|[<>]|[!]|[=]|[()]|\w+/g);
It works for the most part, except for one thing, character such as ^
are not kept. So instead of getting...
300^
I'm getting just...
300
How can I keep every string intact and only split of the delimiters mentioned?
Upvotes: 0
Views: 1103
Reputation: 370679
It sounds like when you match \w+
, you also want to match ^
s in that same matched substring, so make a character set and include ^
in that character set, as well as \w
:
const input_text = '(!false =>stuff <300$$^300 OR <=200 "TEST DATA")';
console.log(
input_text.match(/"[^"]*"|=[<>]|[<>]=|[<>]|[!]|[=]|[()]|[\w^$]+/g)
// ^^^^^
);
If all but the last alternations of the regular expression take care of all the special cases, then another option is to, instead of matching word (and selected special) characters finally, you could match anything but a whitespace character (the initial alternations will take priority, if any match):
const input_text = '(!false =>stuff <300$$^300 OR <=200 "TEST DATA")';
console.log(
input_text.match(/"[^"]*"|=[<>]|[<>]=|[<>]|[!]|[=]|[()]|[^\s]+/g)
// ^^^^^
);
Upvotes: 1