Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function matchMultiplication(str) {
- let pattern = /(-*\d*\s*)\*\s*(-*\d?\.\d{1,2})/g;
- let numbers= str.match(pattern);
- for (let multiplication of numbers) {
- let nums = multiplication.split(/[ \*]+/g);
- let x=Number(nums[0]);
- let y=Number(nums[1]);
- let result=x*y;
- str=str.replace(multiplication,result);
- }
- console.log(str);
- }
- // matchMultiplication('My bill: 2*2.50 (beer); 2* 1.20 (kepab); -2 * 0.5 (deposit).');
- matchMultiplication('My bill is: 4 * 2.50 (beer); 12* 1.50 (kepab); 1 *4.50 (salad); 2 * -0.5 (deposit).');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement