Hi I am trying to write a small script which gets all the amounts in a page(Amazon).
This is what I have come up with to store the values in an array.
var amounts= $('*').text().match(/^\$[-0-9.,]*/);
But this code is not working because in the actual page there is some text/tags before the $ symbol like
<p>$500</p>
Can someone pls correct my code?
I've had luck with this on amazon
var arrayPrices = document.body.textContent.match(/\$\d+\.?\d+/g);
str.match(/^\$[-0-9.,]*/) could be
var arr=str.match/\$\d+(\.\d+)?/g
To match all the integers and (possible) decimal strings that follow a dollar sign.
you can use reggae grouping like this
var str = "$500";
var array = "/\$(\d+)/".exec(str);
alert(array[1]);
in array[0] you will find the String str. In array[1] the first hit and so on.
Related
I have a string that will be formatted something like ___<test#email.com>____ where the underscores is irrelevant stuff I don't need but varys in length. I need to select and store what is between the brackets.
My problem is that all of the sub string solutions I have seen operate off of a hard integer location in the string. But the start and end of the substring I want to select (the brackets) will never be the same.
So I thought if I could use something to find the location of the brackets then feed that to a substring solution that would work. But all of the ways I have found of identifying special characters only reports if there are special characters, not where they are.
Thanks in advance!
based on this answer
var text = '___<test#email.com>____';
var values = text.split(/[<>]+/);
console.log(values); // your values should be at indexes 1, 3, 5, etc...
Here's a regex that should set you on your way.
let string = "asdf asdf asdf as <thing#stuff.com> jl;kj;l kj ;lkj ;lk j;lk";
let myMatches = string.match(/<.*>/g);
let myMatch = myMatches[0].slice(1).slice(0,-1);
The .match function returns an array of matches, so you can find multiple <stuff> entries.
There's probably a way to do it without the slicing, but that's all I've got for now.
With Regex:
var myRe = /<(.*)>/g;
var myArray = myRe.exec("____<asdf>___");
if (myArray)
console.log(myArray[1]);
Regex test here
JSFiddle test here
Currently I have:
var price = '$4000';
var currency = price.match([\$]);
alert(currency);
However this doesn't seem to work. I would like to extract the $ symbol and compare it, if true then fetch some currency comparison and compare currency. What is the correct way to extract symbols via regex?
Thanks
This should work: '$4000'.match(/^\$/).
(It looks for the $ sign at the beginning of the string)
The javascript syntax for regular expression literal uses / at the beginning and at the end of the expression.
If you want to find the dollar sign in any position use:
var currency = price.match(/\$/);
If you want to find the dollar sign at the beginning of the string use:
var currency = price.match(/^\$/);
Here's the documentation about Javascript RegExp
Put your regular expression inside // or quotes:
var price = '$4000';
var currency = price.match(/[\$]/);
alert(currency);
Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems. (c) Jamie Zawinski
I agree that RegEx is more concise but you could do with just '$4000'.indexOf('$'). Like this:
if (price.indexOf('$')>-1) { currency = '$' }
Ok, So I hit a little bit of a snag trying to make a regex.
Essentially, I want a string like:
error=some=new item user=max dateFrom=2013-01-15T05:00:00.000Z dateTo=2013-01-16T05:00:00.000Z
to be parsed to read
error=some=new item
user=max
dateFrom=2013-01-15T05:00:00.000Z
ateTo=2013-01-16T05:00:00.000Z
So I want it to pull known keywords, and ignore other strings that have =.
My current regex looks like this:
(error|user|dateFrom|dateTo|timeFrom|timeTo|hang)\=[\w\s\f\-\:]+(?![(error|user|dateFrom|dateTo|timeFrom|timeTo|hang)\=])
So I'm using known keywords to be used dynamically so I can list them as being know.
How could I write it to include this requirement?
You could use a replace like so:
var input = "error=some=new item user=max dateFrom=2013-01-15T05:00:00.000Z dateTo=2013-01-16T05:00:00.000Z";
var result = input.replace(/\s*\b((?:error|user|dateFrom|dateTo|timeFrom|timeTo|hang)=)/g, "\n$1");
result = result.replace(/^\r?\n/, ""); // remove the first line
Result:
error=some=new item
user=max
dateFrom=2013-01-15T05:00:00.000Z
dateTo=2013-01-16T05:00:00.000Z
Another way to tokenize the string:
var tokens = inputString.split(/ (?=[^= ]+=)/);
The regex looks for space that is succeeded by (a non-space-non-equal-sign sequence that ends with a =), and split at those spaces.
Result:
["error=some=new item", "user=max", "dateFrom=2013-01-15T05:00:00.000Z", "dateTo=2013-01-16T05:00:00.000Z"]
Using the technique above and adapt your regex from your question:
var tokens = inputString.split(/(?=\b(?:error|user|dateFrom|dateTo|timeFrom|timeTo|hang)=)/);
This will correctly split the input pointed out by Qtax mentioned in the comment: "error=user=max foo=bar"
["error=", "user=max foo=bar"]
I am trying to parse a webpage and to get the number reference after <li>YM#. For example I need to get 1234-234234 in a variable from the HTML that contains
<li>YM# 1234-234234 </li>
Many thanks for your help someone!
Rich
currently, your regex only matches if there is a single number before the dash and a single number after it. This will let you get one or more numbers in each place instead:
/YM#[0-9]+-[0-9]+/g
Then, you also need to capture it, so we use a cgroup to captue it:
/YM#([0-9]+-[0-9]+)/g
Then we need to refer to the capture group again, so we use the following code instead of the String.match
var regex = /YM#([0-9]+-[0-9]+)/g;
var match = regex.exec(text);
var id = match[1];
// 0: match of entire regex
// after that, each of the groups gets a number
(?!<li>YM#\s)([\d-]+)
http://regexr.com?30ng5
This will match the numbers.
Try this:
(<li>[^#<>]*?# *)([\d\-]+)\b
and get the result in $2.
I need to capture the price out of the following string:
Price: 30.
I need the 30 here, so I figured I'd use the following regex:
([0-9]+)$
This works in Rubular, but it returns null when I try it in my javascript.
console.log(values[1]);
// Price: 100
var price = values[1].match('/([0-9]+)$/g');
// null
Any ideas? Thanks in advance
Try this:
var price = values[1].match(/([0-9]+)$/g);
JavaScript supports RegExp literals, you don't need quotes and delimiters.
.match(/\d+$/) should behave the same, by the way.
See also: MDN - Creating a Regular Expression
Keep in mind there are simpler ways of getting this data. For example:
var tokens = values[1].split(': ');
var price = tokens[1];
You can also split by a single space, and probably want to add some validation.
Why don't you use this?
var matches = a.match(/\d+/);
then you can consume the first element (or last)
my suggestion is to avoid using $ in the end because there might be a space in the end.
This also works:
var price = values[1].match('([0-9]+)$');
It appears that you escaped the open-perens and therefore the regex is looking for "(90".
You don't need to put quotes around the regular expression in JavaScript.