I have a text similar to the text below. It contains a 4
digits number that follows either digit-
or whitespace
and is followed by either .
, ?
, -digit
or whitespace
.
I need to match all of the digits in the first paragraph but none in the second since those digits do not meet my conditions.
Lorem ipsum 3400-digit, sit amet 5000 consectetur adipisicing elit. Natus, explicabo 6700? Itaque iure ipsum laboriosam, ex nemo delectus iste quia cupiditate digit-9134? Iste nam digit-2456 at voluptate est 8456-digit? At excepturi quis voluptatibus 7500.
Lorem ipsum $5000 dolor sit amet consectetur adipisicing elit. Obcaecati tempora dolorum repellat reiciendis cum soluta deserunt ex voluptatibus, nam illum veniam £5550 quidem aperiam sequi, nostrum sed? Quidem eveniet maiores #5550 autem. https://codepen.io/pen/5000/3454
There are a few similar questions already on StackOverflow. I have gone through some of them(links below), but I still can not do this. Please before marking this question as duplicate, check if your solution finds all the occurrence of the 4 digits number in the first paragraph but none in the second paragraph.
regular expression to match exactly 5 digits
Regular expression to match integers up to 9 digits
RegEx match exactly 4 digits
JavaScript: \\d{4} RegExp allows more than 4 digits
You may use the following pattern:
/(?:\bdigit-|\s|^)(\d{4})(?=[.?\s]|-digit\b|$)/gi
See the regex demo. You need to get all Group 1 values.
Details
(?:\bdigit-|\s|^)
- either digit-
(as a whole word), whitespace or start of string(\d{4})
- Group 1: four digits(?=[.?\s]|-digit\b|$)
- immediately to the right, there must be a .
, whitespace, ?
, -digit
(as a whole word) or end of string. NOTE Without a lookahead, consecutive whitespace-separated matches will be left out.JS demo:
var strs = ["Lorem ipsum 3400-digit, sit amet 5000 consectetur adipisicing elit. Natus, explicabo 6700? Itaque iure ipsum laboriosam, ex nemo delectus iste quia cupiditate digit-9134? Iste nam digit-2456 at voluptate est 8456-digit? At excepturi quis voluptatibus 7500.", "Lorem ipsum $5000 dolor sit amet consectetur adipisicing elit. Obcaecati tempora dolorum repellat reiciendis cum soluta deserunt ex voluptatibus, nam illum veniam £5550 quidem aperiam sequi, nostrum sed? Quidem eveniet maiores #5550 autem. https://codepen.io/pen/5000/3454" ];
var rx = /(?:\bdigit-|\s|^)(\d{4})(?=[.?\s]|-digit\b|$)/gi;
for (var s of strs) {
var m, res =[];
while(m=rx.exec(s)) {
res.push(m[1]);
}
console.log(res);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With