1

I have some text in text input that contain numbers with a pattern in whole of text and want to find all number with this pattern and recalculate and replace in text

 .... val="2xf" 
 .........
 vc="2.6xf"
 ...... 
 value="1.2xf"
 .....

find all number with xf and change it with a formula

for example: find all matched pattern and replace by sum with 5
2xf =>change to 7xf , (2+5=7)
2.6xf change to 7.6xf , (2.6+5=7.6)
1.2xf change to 6.2xf , (1.2+5=6.2)

 .... val="7xf" 
 .........
 vc="7.6xf"
 ...... 
 value="6.2xf"

how can do it?

Mohammad Javad
  • 573
  • 1
  • 7
  • 29

1 Answers1

2

You can solve this problem with a help of RegEx and replace method of String object:

value = value.replace(/(\d+)(.*xf)/g, function(_, number, other) {
    return (+number + 5).toString() + other;
});