Capture Four Numbers With One Javascript Regular Expression
Solution 1:
You simply need to capture a double number like 0.00
, this is the Regex (\d.\d{2})
you need:
var text = 'PAY VALUES TO SUM - WITHOUT NOTHING: 0.13 13º WORD WORD: 0.00';
text += 'PAY VALUES TO SUM - ADD. 50 YEARS: 3.85 WORD WORD WORD: 0.00';
text += 'PAY VALUES TO SUM - ADD. 70 YEARS: 9.02 WORD WORD WORD: 0.00';
text += 'PAY VALUES TO SUM - ADD. 80 YEARS: 5.21 WORD WORD WORD WORD WORD WORD WORD: 0.00';
var re = /(\d.\d{2})/g;
var m;
while ((m = re.exec(text)) !== null) {
if (m.index === re.lastIndex) {
re.lastIndex++;
}
document.write(m[0]+" ");
}
And this is a regex101 DEMO.
Note: I used document.write()
only for test, avoid using it.
EDIT:
This is the new Regex (PAY VALUES TO SUM - (?:WITHOU NOTHING|ADD. \d{2} YEARS): (\d.\d{2}))
of the second attempt to get what you need, I hope it is what you are looking for:
var text = 'PAY VALUES TO SUM - WITHOU NOTHING: 0.13 13º WORD WORD: 0.00';
text += 'PAY VALUES TO SUM - ADD. 50 YEARS: 3.85 WORD WORD WORD: 0.00';
text += 'PAY VALUES TO SUM - ADD. 70 YEARS: 9.02 WORD WORD WORD: 0.00';
text += 'PAY VALUES TO SUM - ADD. 80 YEARS: 5.21 WORD WORD WORD WORD WORD WORD WORD: 0.00';
var re = /(PAY VALUES TO SUM - (?:WITHOU NOTHING|ADD. \d{2} YEARS): (\d.\d{2}))/g;
var m;
while ((m = re.exec(text)) !== null) {
if (m.index === re.lastIndex) {
re.lastIndex++;
}
document.write(m[0] + "<br>");
}
Solution 2:
Start by putting your keys into an array:
var keys = [
'PAY VALUES TO SUM - WITHOU NOTHING:','PAY VALUES TO SUM - ADD. 50 YEARS:','PAY VALUES TO SUM - ADD. 70 YEARS:','PAY VALUES TO SUM - ADD. 80 YEARS:'
];
Now compile a regular expression that looks for a number after each key. You can use [0-9.]+
to match a sequence of one or more characters in the class [0-9.]
, which consists of the digits 0
through 9
and the .
(period). That will suffice to match numbers that look like 0.00
and 314.159
.
Here is a regular expression that uses [0-9.]+
to extract a number after each key:
var regex = RegExp(keys[0] + '\\s+([0-9.]+).*?' +
keys[1] + '\\s+([0-9.]+).*?' +
keys[2] + '\\s+([0-9.]+).*?' +
keys[3] + '\\s+([0-9.]+).*?', 'gi');
Note that \\s+
matches one or more spaces after each key. You can change it to \\s*
if you want to match zero or more spaces.
We are using .*?
to match the shortest possible sequence of characters before the next key. The 'gi'
flags make the regular expression match globally (g
) and without regard to case (i
).
Before executing the regular expression, it's a good idea to replace all invisible whitespace with visible spaces. That's because .*
does not match line-ending characters.
text = text.replace(/\s/g, ' ');
The snippet below demonstrates this approach with the sample text and keys that you gave in your question.
functionprint(s) {
document.write(s + '<br />');
}
var keys = [
'PAY VALUES TO SUM - WITHOU NOTHING:',
'PAY VALUES TO SUM - ADD. 50 YEARS:',
'PAY VALUES TO SUM - ADD. 70 YEARS:',
'PAY VALUES TO SUM - ADD. 80 YEARS:'
];
// Compile a regular expression that looks for a number after each key.var regex = RegExp(keys[0] + '\\s+([0-9.]+).*?' +
keys[1] + '\\s+([0-9.]+).*?' +
keys[2] + '\\s+([0-9.]+).*?' +
keys[3] + '\\s+([0-9.]+).*?', 'gi');
var text = document.getElementById('content').innerHTML;
// Replace newlines and other invisible whitespace with visible spaces.
text = text.replace(/\s/g, ' ');
// Find and display all matches.var count = 0;
while (true) {
var match = regex.exec(text);
if (match === null) {
break;
}
++count;
print('match ' + count + ': ' +
[match[1], match[2], match[3], match[4]].join(' '));
}
body {
font-family: sans-serif;
}
#content {
display: none;
}
<divid="content">
PAY VALUES TO SUM - WITHOU NOTHING: 0.00 13º WORD WORD: 0.00
PAY VALUES TO SUM - ADD. 50 YEARS: 0.00 WORD WORD WORD: 0.00
PAY VALUES TO SUM - ADD. 70 YEARS: 0.00 WORD WORD WORD: 0.00
PAY VALUES TO SUM - ADD. 80 YEARS: 0.00 WORD WORD WORD WORD WORD WORD WORD: 0.00
</div>
Solution 3:
If I understood correctly, you only need the 1st occurrence of each xx.xx number after the :
in each line, as numbers can occur also after the 1st occurence.
If that is the case, then this is your regular expression:
/^.*?:\s*([\d\.]+).*/gm
And this is a demo: https://regex101.com/r/gT9tJ0/1
Solution 4:
Try this: you can use this short regex :/PAY\s+VALUES\s+TO\s+SUM\s+[^:]+:\s*([\d.]+)/gmi
var re = /PAY\s+VALUES\s+TO\s+SUM\s+[^:]+:\s*([\d.]+)/gmi;
var str = 'var text = \'PAY VALUES TO SUM - WITHOU NOTHING: 0.00 13º WORD WORD: 0.00\' +\ntext += \'PAY VALUES TO SUM - ADD. 50 YEARS: 0.00 WORD WORD WORD: 0.00\';\ntext += \'PAY VALUES TO SUM - ADD. 70 YEARS: 0.00 WORD WORD WORD: 0.00\';\ntext += \'PAY VALUES TO SUM - ADD. 80 YEARS: 0.00 WORD WORD WORD WORD WORD WORD WORD: 0.00\';\n';
var m;
while ((m = re.exec(str)) !== null) {
if (m.index === re.lastIndex) {
re.lastIndex++;
}
// View your result using the m-variable.document.write(m[1]+ ' ');
}
Post a Comment for "Capture Four Numbers With One Javascript Regular Expression"