Advertisement
YavorJS

16. *Match Multiplication

Jun 4th, 2017
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.58 KB | None | 0 0
  1. function matchMultiplication(str) {
  2. let pattern = /(-*\d*\s*)\*\s*(-*\d?\.\d{1,2})/g;
  3. let numbers= str.match(pattern);
  4.  
  5. for (let multiplication of numbers) {
  6. let nums = multiplication.split(/[ \*]+/g);
  7. let x=Number(nums[0]);
  8. let y=Number(nums[1]);
  9. let result=x*y;
  10. str=str.replace(multiplication,result);
  11. }
  12. console.log(str);
  13. }
  14. // matchMultiplication('My bill: 2*2.50 (beer); 2* 1.20 (kepab); -2 * 0.5 (deposit).');
  15. 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