This question already has answers here:
How do you access the matched groups in a JavaScript regular expression?
(23 answers)
Closed 6 years ago.
I have the following regex in javascript. I want to match a string so its product (,|x) quantity, each price. The following regex does this; however it returns an array with one element only. I want an array which returns the whole matched string first followed by product, quantity, each price.
// product,| [x] quantity, each price:
var pQe = inputText.match(/(^[a-zA-z -]+)(?:,|[ x ]?) (\d{0,3}), (?:each) ([£]\d+[.]?\d{0,2})/g);
(,|x) means followed by a comma or x, so it will match ice-cream, 99, $99 or ice-cream x 99, $99
How can I do that?
Use the exec function of the RegExp object. For that, you'll have to change the syntax to the following:
var pQe = /(^[a-z ... {0,2})/g.exec(inputText)
Element 1 of the returned array will contain the first element of the match, element 2 will contain the second, and so on. If the string doesn't match the regular expression, it returns null.
Related
This question already has answers here:
Test If String Contains All Characters That Make Up Another String
(4 answers)
Closed 2 years ago.
I have been struggling with a regex pattern to find out if a string contains all characters in a set of characters.
e.g I want to test if "sheena" contains esnh, I should get a match but if I want to test if "sheena" contains esnk, I should not get a match because there exist no k in "sheena" even thought esn exists.
To solve this problem just use the every method.
const matchString = (str, matcher) => [...matcher].every(n => str.includes(n));
console.log(matchString('sheena', 'eshn'));
console.log(matchString('sheena', 'eshk'));
This question already has answers here:
Get Substring between two characters using javascript
(24 answers)
Closed 2 years ago.
String example: "{something}" or "{word: something}"
What I need to do is get 'something', so the text between two specific parts of the string, in these case {-} and {word:-} (The 'something' part can change in every string, I never know what it is).
I tried using string.find() or regex but I didn't come up with a conclusion. What's the quickest and best way to do this?
What you need is a capture group inside a regex.
> const match = /\{([^}]+)\}/.exec('{foo}')
> match[1]
'foo'
The stuff in the parens, ([^}]+), matches any character but }, repeated at least once. The parens make it be captured; the first captured group is indexed as match[1].
This question already has answers here:
match() returns array with two matches when I expect one match
(2 answers)
Closed 5 years ago.
I cant understand why this code snippet return an array on two strings "BEARING" instead of only a string "BEARING. Any ideas?
const cleanedString = "ANGULAR CONTACT (ROLLING) BEARING"
const noun = cleanedString.match(/\b(\w+)$/);
console.log(noun);
You need to use the global /g flag:
const cleanedString = "ANGULAR CONTACT (ROLLING) BEARING"
const noun = cleanedString.match(/\b(\w+)$/g);
console.log(noun);
From String.prototype.match() [MDN]:
If the regular expression does not include the g flag, str.match() will return the same result as RegExp.exec().
It returns an array of 2 which signify
Full Match of the string
String matched in the first capturing group
You can make it a non capturing group by
const cleanedString = "ANGULAR CONTACT (ROLLING) BEARING"
const noun = cleanedString.match(/\b(?:\w+)$/);
console.log(noun);
where ?: signifies that the group would be non capturing
By default match returns the string that matched as the first value.
By putting parens in your regex, you asked for a part of the matched string to be returned (which happens to be the same in this case).
So if your regex had been this:
/^(\w+).*\b(\w+)$/
You would have 3 strings returned
The whole string
ANGULAR
BEARING
This question already has answers here:
split string only on first instance of specified character
(21 answers)
Closed 6 years ago.
I'm having a string like below that i would like to split only on the first ,. so that if i for instance had following string Football, tennis, basketball it would look like following array
["football", "tennis, basketball"]
This should do it
var array = "football, tennis, basketball".split(/, ?(.+)?/);
array = [array[0], array[1]];
console.log(array);
Inspiration: split string only on first instance of specified character
EDIT
I've actually found a way to reduce the above function to one line:
console.log("football, tennis, basketball".split(/, ?(.+)?/).filter(Boolean));
.filter(Boolean) is used to trim off the last element of the array (which is just an empty string).
This question already has answers here:
Why capturing group results in double matches regex
(2 answers)
Closed 8 years ago.
I've got the following string, defined in my Javascript code:
var s = "10px solid #101010";
Now, I want to get only the numeric initial part. For this purporse, I tried the following:
s.match(/^([0-9]+)/g);
It works like a charm, and the result was ['10'].
After this test, I also tried to remove the g modifier, as follows:
s.match(/^([0-9]+)/);
I expected the same result, but I was wrong. The result is an array with two solutions: ['10', '10'].
Can someone explain me why? What is the meaning of the last element of the array, in the second case?
'/g' returns all matched ones, It wont stop at first matching point
'g '- Perform a global match (find all matches rather than stopping after the first match)
https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/RegExp