-3

This code can only retrieve 1.58, but I also need to match 3.45. What should I change on my Regexp to do this?

let s = '1.58х3.45';
re = /[0-9/.]+/;
found = s.match(re);
console.log(found);
zer00ne
  • 41,936
  • 6
  • 41
  • 68
Eniso
  • 29
  • 4
  • 2
    Use the `g` flag, https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/global#using_global – CBroe Oct 13 '22 at 09:40
  • 1
    Dupe: https://stackoverflow.com/q/41065204/519413 – Rory McCrossan Oct 13 '22 at 09:42
  • The question is not really clear. Do you want to find only numbers after a specific symbol or all numbers? What's the expected result for `1.58x3.4/5y1.2/5`? Why is `1.58` part of the result? It's not after a special symbol. – jabaa Oct 13 '22 at 10:01

2 Answers2

-1

I'm not exactly sure what you need to find, so let me know if I missed something:

Criteria

  1. A non-number character either a literal dot . or a forward slash /.
  2. One or more numbers must follow that symbol.
  3. Optionally, one or more numbers may precede that symbol as well.
  4. Multiple matches must be made if there are any.

RegExp

/\d*[./]\d+/g
Segment Description
/
Begin RegExp
\d*
Zero or more numbers
[./]
Either a literal dot . or a forward slash /
\d+
One or more numbers
/
End RegExp
g
Global flag which makes method continue after the first match

const str = `23.6x1.25 . .2 /2 5.66 .. 5/ 100`;
const rgx = /\d*[./]\d+/g;

const res = str.match(rgx);
console.log(res);
zer00ne
  • 41,936
  • 6
  • 41
  • 68
-1

Here is how you can extract two numbers from a string with different delimiters between the numbers:

[
  '1.58х3.45',
  '1.58&3.45',
  '1.58 x 3.45',
  'x'
].forEach(str => {
  let m = str.match(/([0-9\.]+)[^0-9\.]+([0-9\.]+)/);
  console.log(str + ' ==> ' + (m ? 'found: ' + m[1] + ' & ' + m[2] : '(no match)'));
});
Output:
1.58х3.45 ==> found: 1.58 & 3.45
1.58&3.45 ==> found: 1.58 & 3.45
1.58 x 3.45 ==> found: 1.58 & 3.45
x ==> (no match)

Explanation:

  • ([0-9\.]+) -- expect a number (1+ digits or dots), and capture it
  • [^0-9\.]+ -- expect non-number characters
  • ([0-9\.]+) -- expect a number (1+ digits or dots), and capture it
Peter Thoeny
  • 7,379
  • 1
  • 10
  • 20
  • I downvoted because the question is _"How to take numbers from string after special sumbol?"_ `1.58` doesn't follow a special symbol. This answer doesn't answer the question. When this answer was written, there was a list of potential special symbols in the question. – jabaa Oct 15 '22 at 14:31
  • @jabaa: The title is not clear, I think there is an English language issue. The OP clarifies with "This code can only retrieve 1.58, but I also need to match 3.45", referring to input string `'1.58х3.45'`. Hence "How to take numbers from string after special sumbol?" refers to the "special symbol `x`. Agree? – Peter Thoeny Oct 17 '22 at 17:40
  • An answer, that doesn't answer the actual question isn't helpful for future users with similar problems, even if it helps the OP to solve their problem. If the question is unclear, it should be clarified and not answered. – jabaa Oct 17 '22 at 17:54
  • @jabaa: so we agree to disagree. IMHO I answered the question on "How to take numbers from string after special sumbol?" where symbol is the `x` in his example. – Peter Thoeny Oct 18 '22 at 20:18