I'm looking to match individual characters from a calculation string such as:
(123+321)*1.15
The list of characters I'd like to match is:
0-9, ., +, -, *, /, (, ), %
Each character of the string will be passed into a function individually. I think I have the starting point (which works great with the numbers):
if (character.match(/[0-9]{1}/) !== null) {
// do something...
}
I'm not quite sure how to add the remaining characters though (I've always found regex confusing, even after reading countless articles on the subject).
-
You'll have more luck parsing it character-by-character.Blender– Blender2013年04月15日 19:49:07 +00:00Commented Apr 15, 2013 at 19:49
-
You just want to check if a character is one of those?Dogbert– Dogbert2013年04月15日 19:50:21 +00:00Commented Apr 15, 2013 at 19:50
-
@Dogbert Yes. That's all.Phil– Phil2013年04月15日 19:54:06 +00:00Commented Apr 15, 2013 at 19:54
1 Answer 1
You can just add the characters you want with the 0-9
, like
character.match(/[0-9.+\-*/()%]/)
The only character needing to be escaped in the RegExp is -
, as it normally means a range of characters.
There's no need of {1}
as the default is to match 1 instance.