regex - JavaScript match ( followed by a digit -


in string n+n(n+n), n stands number or digit, i'd match ( , replace *(, if followed number or digit.

examples:

  • i'd change 2+22(2+2) 2+22*(2+2),
  • i'd change -1(3) -1*(3),
  • 4+(5/6) should stay is.

this have:

var str = '2+2(2+2)'.replace(/^[0-9]\(/g, '*('); 

but doesn't work. in advance.

remove ^, , group digits:

'2+2(2+2)'.replace(/([0-9])\(/g, '$1*(') '2+2(2+2)'.replace(/(\d)\(/g, '$1*(')    //another option: [0-9] = \d 

suggestion: 2. valid number (= 2). following regexp removes dot between number , parenthesis.

'2+2(2+2)'.replace(/(\d\).?\(/g, '$1*(') //2.(2+2) = 2*(2+2) 

parentheses create group, can referenced using $n, n index of group: $1.

you started regexp ^..., means: match part of string starts .... behaviour not intended.


Comments

Popular posts from this blog

objective c - Change font of selected text in UITextView -

php - Accessing POST data in Facebook cavas app -

c# - Getting control value when switching a view as part of a multiview -