Lukas Oppermann
Lukas Oppermann

Reputation: 2938

Regex to match digits only if not followed or proceeded by letters in Javascript

I would like to match digits but not when they are within words (in JavaScript).

The following should match:

1
1,2
1.5-4 (matches 1.5 & 4 separately)
(1+3) (matches 1 & 3 separately)
=1;

The following should NOT match:

FF3D
3deg

I thought I could solve with with a negative lookahead, like so: (?![A-Za-z]+)([0-9]+[\.|\,]?[0-9]?) but it does not work.

How can I best solve this? Thanks.

Upvotes: 1

Views: 1350

Answers (3)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626845

2021 update:

Since lookbehind support has grown considerably, it makes sense to use a lookbehind based solution:

/(?<![a-z])\d*[.,]?\d+(?![a-z])/gi         # ASCII only
/(?<!\p{L})\p{N}*[.,]?\p{N}+(?!\p{L})/giu  # Unicode-aware

See the regex demo. Please track the lookbehind and Unicode property class support here.

Details

  • (?<![a-z]) - no ASCII letter (or any Unicode letter if \p{L} is used) allowed immediately to the left of the current location
  • \d*[.,]?\d+
  • (?![a-z]) - no ASCII letter (or any Unicode letter if \p{L} is used) allowed immediately to the right of the current location.

Original answer

In order to match any standalone integer or float numbers with dot or comma as decimal separator you need

/(?:\b\d+[,.]|\B[.,])?\d+\b/g

See the regex demo. The point here is that you cannot use a word boundary \b before a . since it will invalidate all matches like .55 (only 55 will be matched).

Details:

  • (?:\b\d+[,.]|\B[.,])? - either of the two alternatives:
    • \b\d+[,.] - a word boundary (there must be a non-word char before or start of string), then 1+ digits, and then a . or ,
    • | - or
    • \B[.,] - a position other than word boundary (only a non-word char or start of string) and then a . or ,
  • \d+ - 1+ digits
  • \b - a word boundary.

const regex = /(?:\b\d+[,.]|\B[.,])?\d+\b/g;
const str = `.455 and ,445 44,5345 435.54 4444
1
1,2
1.5-4
(1+3)
=1;

FF3D
3deg`;
console.log(str.match(regex));

If you need to also add support for the exponent use:

/(?:\b\d+[,.]|\B[.,])?\d+(?:e[-+]?\d+)?\b/ig

Upvotes: 1

Jobelle
Jobelle

Reputation: 2834

Try This

var pattern= /([\d.]+)/;

https://regex101.com/r/q1qDHV/1

Upvotes: 0

anubhava
anubhava

Reputation: 785156

I would like to match digits but not when they are within words.

You can use look arounds in your regex:

\b\d*[,.]?\d+\b
  • \b is for word boundary

RegEx Demo

Upvotes: 3

Related Questions