skip to Main Content

Upon extracting context from webpage, I was left with a string "price: $30.00" which I stored as "x." all I needed was "30.00"(numbers only.) I tried replacing using x.replace(), but it was all in vain. I would appreciate if anyone can help me write the snippet.

I tried using storeEval, only to find that it had been removed. Replace was the only option I had and I tried using it only to enouter difficulties.

2

Answers


  1. // input
    var x = "price: $30.00";
    
    //Use regex
    var numbers = x.match(/d+(.d+)?/);
    
    if (numbers) {
      var extractedNumber = numbers[0];
      console.log(extractedNumber);
    } else {
      console.log("Numeric values not detected in the provided string.");
    }
    
    Login or Signup to reply.
  2. There is another (simpler) way:

    var x = "price: $30.00";
    var extractedNumber = x.split("$")[1];
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search