0

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).

asked Apr 15, 2013 at 19:47
3
  • You'll have more luck parsing it character-by-character. Commented Apr 15, 2013 at 19:49
  • You just want to check if a character is one of those? Commented Apr 15, 2013 at 19:50
  • @Dogbert Yes. That's all. Commented Apr 15, 2013 at 19:54

1 Answer 1

2

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.

answered Apr 15, 2013 at 19:54

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.