Prasanth Klr
Prasanth Klr

Reputation: 131

Regex match with 3 condition?

I will creating math program .If i wish to solve i need separate to the equation using with regex. For example:

     `10x-10y2+100x-100k=100`   

the format will separate with regex output: "10x" ,"100x" ,"100k" ,"10y2","100"

I have already code for that with separate match function

There are:

           for num[a-z]num : ` /([\-+])?\s*(\d+)?([a-z]?(\d+))/g`
           for num[a-z]    : ` /([\-+])?\s*(\d+)?([a-z]?(\d+))/g`
           fon num         : `/\b[+-]?[\d]+\b/g`

I need all this three match function within one regex match function.some one help to combine the code with single regex expression

Note :i need match function only not a split beacause i apply that regex expression into parser

Thank You.

Upvotes: 1

Views: 262

Answers (4)

Whothehellisthat
Whothehellisthat

Reputation: 2152

/(?:0|[1-9]\d*)?(?:[a-z]+)?(?:\^(?:0|[1-9]\d*))?/g

// finds:
//        vvv vvvvv vvvv vvvv vvvvv vv vvv       v vvv
          10x-10y^2+100x-100k=100^4+xy+100+100y2+0-1^0
// doesn't find:                           ^^^^^
  • (?:0|[1-9]\d*)? 0, OR 1-9 then zero or more numbers. Optional.
  • (?:[a-z]+)? Optional one or more lowercase letters.
  • (?:\^[1-9]\d*)? Optional power.
    • \^ Literal text.
    • (?:0|[1-9]\d*) Zero, OR 1-9 then zero or more numbers.

If I've missed anything let me know and I'll incorporate it.

Upvotes: 1

revo
revo

Reputation: 48711

Split on symbols:

"10x-10y2+100x-100k=100".split(/[-+=]/);

Output:

["10x", "10y2", "100x", "100k", "100"]

If you need to use match() method I suggest same approach:

"10x-10y2+100x-100k=100".match(/[^-+=]+/g);

Output is the same.

Upvotes: 2

Daniel Tran
Daniel Tran

Reputation: 6171

Try this:

 s.match(/\d+([A-Za-z]\d*)?/g)

Upvotes: 0

hsz
hsz

Reputation: 152216

Just try with following regex:

/(\d+[a-z]?\d*)/g

example

Upvotes: 0

Related Questions