I would like to ignore a number that represents a price anywhere in text.
It should match
3.5 mm
-2
#1
It should ignore
$3.50
So far I have a Regex for Javascript
([^\$¢£]([0-9]+(\.[0-9]+)?))([^a-zA-Z]|$)
but this will still match the 3.50 just exluding the dollar sign. What is missing to ignore the the whole number?
EDIT: For testing https://regex101.com/r/9SLNo2/1
You need to anchor the match to the start of the string by adding a ^
to the front. I do not really understand what you are trying to accomplish by the most of your regular expression though.
If you just want anything that doesn't start with a currency symbol, try ^[^\\$¢£].*$
. Anything that doesn't contain any currency symbol, try ^[^\\$¢£]*$
. Anything that contains a number (optionally decimal, always contain the whole part like you seem to intend), surrounded optionally by "non-currency symbols", try ^[^\\$¢£]*[0-9]+(\\.[0-9]+)?[^\\$¢£]*$
.
Unfortunately, lookbehinds are not supported in JS
but you could use a "trick":
match anything you don't want but capture anything you do want :
junk_a|junk_b|junk_c|(interesting_stuff)
So here with your specific example:
[$¢£]\s*-?\d+(?:\.\d+)?|(-?\d+(?:\.\d+)*)
# ^^^^^ junk part ^^^^^
interesting_stuff
) is set:
let data = 'lorem ipsum 3.5 mm -2 #1 lorem ipsum $3.50 lorem ipsum'; let regex = /[\\$¢£]\\s*\\d+(?:\\.\\d+)*|(-?\\d+(?:\\.\\d+)*)/g; let interesting = []; while ((match = regex.exec(data)) !== null) { if (typeof(match[1]) != "undefined") { interesting.push(match[1]); } } console.log(interesting);
See a demo on regex101.com (needs to be tweaked for the units).
You could just ignore values that begin with a dollar sign...
var values = ['3.5 mm', '-2', '#1', '$3.50']; var regex = new RegExp('^\\\\$'); var res = values.filter(function(val) { if (val.match(regex)) { console.log(val, 'skip'); } else { return val; } }) console.log(res);
function check() { // if you want to match only numbers at the bigining as well as # var a = document.getElementById("test").value; var remove = /^-?\\d*\\.?\\d+|#/; var b = a.match(remove); if (!b) console.log("ignore"); else console.log("true"); } function check2() { // if you want to ignore first charcter if match these $,¢ and £ and allow others var a = document.getElementById("test").value; var remove = ['$', '¢', '£']; var b = a.charAt(0); if (remove.indexOf(b) != -1) console.log("ignore"); else console.log("true"); }
<input type="text" onblur="check();check2()" id="test">
As an alternative, maybe you could match the numbers that represent a price and replace the match with an empty string:
var pattern = /[£$¢]\\d+(?:\\.\\d+)? */g; var text = `3.5 mm This is 3.5 mm. -2 This is -2 and test This is #1 and test #1 $3.50 $3.50 This is $3.50. This is $3.50 a test This is a £100000 test and $5000.00 test. This is a ¢100000 test`; console.log(text.replace(pattern, ""));
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.