I have following string:
text = '20 as a % of 50'
I need to replace it using a regular expression, the result should be:
'20 / 50 * 100'
How can I do this?
I have following string:
text = '20 as a % of 50'
I need to replace it using a regular expression, the result should be:
'20 / 50 * 100'
How can I do this?
I created an example here: http://regexr.com?2tfam
You can match with (\d+) as a % of (\d+) and replace with $1 / $2 * 100.
var a="text = '20 as a % of 50'";
alert(a.replace(/(\d+) as a % of (\d+)/, '$1 / $2 * 100'));
I also created a jsFiddle.
EDIT: If your text between the numbers will change, you can use this regex:
(\d+)[%a-zA-Z ]+(\d+)
It assumes that the operator is % and between the numbers only letters and spaces can occur.