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?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
I created an example here: http://regexr.com?2tfam
You can match with
(\d+) as a % of (\d+)and replace with$1 / $2 * 100.I also created a jsFiddle.
EDIT: If your text between the numbers will change, you can use this regex:
It assumes that the operator is % and between the numbers only letters and spaces can occur.