Extract a specific word from string in Javascript - javascript

#anotherdata=value#iamlookingforthis=226885#id=101&start=1
Given the string above how could I extract "iamlookingforthis=226885" in the string? value of it might change as this is dynamic. So, other instance might be "iamlookingforthis=1105". The location/sequence might also change, it could be in the middle or last part.
Thank you in advance.

You can use Regex to match a specific text.
Like this for example
var str = '#anotherdata=value#iamlookingforthis=226885#id=101&start=1';
var value = str.match(/#iamlookingforthis=(\d+)/i)[1];
alert(value); // 226885
Explanation from Regex101:
#iamlookingforthis= matches the characters #iamlookingforthis= literally (case insensitive)
1st Capturing Group (\d+)
\d+ matches a digit (equal to [0-9])
+ Quantifier — Matches between one and unlimited times, as many times as possible, giving back as needed (greedy)
Global pattern flags
i modifier: insensitive. Case insensitive match (ignores case of [a-zA-Z])
See
RegExp on MDN
Regex 101 - try regex and see the explanation of it and results
Another alternative would be to split the string. You could split it by #|?|&.
var str = '#anotherdata=value#iamlookingforthis=226885#id=101&start=1';
var parts = str.split(/[#\?&]/g); // split the string with these characters
// find the piece with the key `iamlookingforthis`
var filteredParts = parts.filter(function (part) {
return part.split('=')[0] === 'iamlookingforthis';
});
// from the filtered array, get the first [0]
// split the value and key, and grab the value [1]
var iamlookingforthis = filteredParts[0].split('=')[1];
alert(iamlookingforthis); // 226885

Here's a snippet:
var str = '#anotherdata=value#iamlookingforthis=226885#id=101&start=1';
var extracted = str.split("#").find(function(v){
return v.indexOf("iamlookingforthis") > -1;
});
alert(extracted); // iamlookingforthis=226885

Related

regex match not outputting the adjacent matches javascript

i was experimenting on regex in javascript. Then i came across an issue such that let consider string str = "+d+a+", I was trying to output those characters in the string which are surrounded by +, I used str.match(/\+[a-z]\+/ig), so here what I'm expecting is ["+d+","+a+"], but what i got is just ["+d+"], "+a+" is not showing in the output. Why?
.match(/.../g) returns all non-overlapping matches. Your regex requires a + sign on each side. Given your target string:
+d+a+
^^^
^^^
Your matches would have to overlap in the middle in order to return "+a+".
You can use look-ahead and a manual loop to find overlapping matches:
var str = "+d+a+";
var re = /(?=(\+[a-z]\+))/g;
var matches = [], m;
while (m = re.exec(str)) {
matches.push(m[1]);
re.lastIndex++;
}
console.log(matches);
With regex, when a character gets consumed with a match, then it won't count for the next match.
For example, a regex like /aba/g wouldn't find 2 aba's in a string like "ababa".
Because the second "a" was already consumed.
However, that can be overcome by using a positive lookahead (?=...).
Because lookaheads just check what's behind without actually consuming it.
So a regex like /(ab)(?=(a))/g would return 2 capture groups with 'ab' and 'a' for each 'aba'.
But in this case it just needs to be followed by 1 fixed character '+'.
So it can be simplified, because you don't really need capture groups for this one.
Example snippet:
var str = "+a+b+c+";
var matches = str.match(/\+[a-z]+(?=\+)/g).map(function(m){return m + '+'});
console.log(matches);

Reduce multiple occurences of any non-alphanumeric characters in string down to one, **NOT REMOVE**

I've been searching for a solution but almost every one that I've come across with is about replacing a matching pattern with a previously known character.
For example:
var str = 'HMQ 2.. Elizabeth';
How do we catch multiple occurences of that dots in the string and replace them with only one? And it's also not specific to dots but any non-alphanumeric characters that we don't know which. Thank you.
Use a backreference. \1 in the regex refers to the first match group in the expression.
var str = 'HMQ 2.. Elizabetttth .';
var regex = /([^A-Za-z0-9])\1+/g;
var trimmed = str.replace(regex, "$1");
console.log( trimmed );

javascript regex to return letters only

My string can be something like A01, B02, C03, possibly AA18 in the future as well. I thought I could use a regex to get just the letters and work on my regex since I haven't done much with it. I wrote this function:
function rowOffset(sequence) {
console.log(sequence);
var matches = /^[a-zA-Z]+$/.exec(sequence);
console.log(matches);
var letter = matches[0].toUpperCase();
return letter;
}
var x = "A01";
console.log(rowOffset(x));
My matches continue to be null. Am I doing this correctly? Looking at this post, I thought the regex was correct: Regular expression for only characters a-z, A-Z
You can use String#replace to remove all non letters from input string:
var r = 'AA18'.replace(/[^a-zA-Z]+/g, '');
//=> "AA"
Your main issue is the use of the ^ and $ characters in the regex pattern. ^ indicates the beginning of the string and $ indicates the end, so you pattern is looking for a string that is ONLY a group of one or more letters, from the beginning to the end of the string.
Additionally, if you want to get each individual instance of the letters, you want to include the "global" indicator (g) at the end of your regex pattern: /[a-zA-Z]+/g. Leaving that out means that it will only find the first instance of the pattern and then stop searching . . . adding it will match all instances.
Those two updates should get you going.
EDIT:
Also, you may want to use match() rather than exec(). If you have a string of multiple values (e.g., "A01, B02, C03, AA18"), match() will return them all in an array, whereas, exec() will only match the first one. If it is only ever one value, then exec() will be fine (and you also wouldn't need the "global" flag).
If you want to use match(), you need to change your code order just a bit to:
var matches = sequence.match(/[a-zA-Z]+/g);
To return an array of separate letters remove +:
var matches = sequence.match(/[a-zA-Z]/g);
You're confused about what the goal of the other question was: he wanted to check that there were only letters in his string.
You need to remove the anchors ^$, who match respectively the beginning and end of the string:
[a-zA-Z]+
This will match the first of letters in your input string.
If there might be more (ie you want multiple matches in your single string), use
sequence.match(/[a-zA-Z]+/g)
This /[^a-z]/g solves the problem. Look at the example below.
function pangram(str) {
let regExp = /[^a-z]/g;
let letters = str.toLowerCase().replace(regExp, '');
document.getElementById('letters').innerHTML = letters;
}
pangram('GHV 2## %hfr efg uor7 489(*&^% knt lhtkjj ngnm!##$%^&*()_');
<h4 id="letters"></h4>
You can do this:
var r = 'AA18'.replace(/[\W\d_]/g, ''); // AA
Also can be done by String.prototype.split(regex).
'AA12BB34'.split(/(\d+)/); // ["AA", "12", "BB", "34", ""]
'AA12BB34'.split(/(\d+)/)[0]; // "AA"
Here regex divides the giving string by digits (\d+)

Regular expression with asterisk quantifier

This documentation states this about the asterisk quantifier:
Matches the preceding character 0 or more times.
It works in something like this:
var regex = /<[A-Za-z][A-Za-z0-9]*>/;
var str = "<html>";
console.log(str.match(regex));
The result of the above is : <html>
But when tried on the following code to get all the "r"s in the string below, it only returns the first "r". Why is this?
var regex = /r*/;
var str = "rodriguez";
console.log(str.match(regex));
Why, in the first example does it cause "the preceding" character/token to be repeated "0 or more times" but not in the second example?
var regex = /r*/;
var str = "rodriguez";
The regex engine will first try to match r in rodriguez from left to right and since there is a match, it consumes this match.
The regex engine then tries to match another r, but the next character is o, so it stops there.
Without the global flag g (used as so var regex = /r*/g;), the regex engine will stop looking for more matches once the regex is satisfied.
Try using:
var regex = /a*/;
var str = "cabbage";
The match will be an empty string, despite having as in the string! This is because at first, the regex engine tries to find a in cabbage from left to right, but the first character is c. Since this doesn't match, the regex tries to match 0 times. The regex is thus satisfied and the matching ends here.
It might be worth pointing out that * alone is greedy, which means it will first try to match as many as possible (the 'or more' part from the description) before trying to match 0 times.
To get all r from rodriguez, you will need the global flag as mentioned earlier:
var regex = /r*/g;
var str = "rodriguez";
You'll get all the r, plus all the empty strings inside, since * also matches 'nothing'.
Use global switch to match 1 or more r anywhere in the string:
var regex = /r+/g;
In your other regex:
var regex = /<[A-Za-z][A-Za-z0-9]*>/;
You're matching literal < followed by a letter followed by 0 or more letter or digits and it will perfectly match <html>
But if you have input as <foo>:<bar>:<abc> then it will just match <foo> not other segments. To match all segments you need to use /<[A-Za-z][A-Za-z0-9]*>/g with global switch.

Regex non capturing groups in javascript

I'm a bit rusty on my regex and javascript. I have the following string var:
var subject = "javascript:loadNewsItemWithIndex(5, null);";
I want to extract 5 using a regex. This is my regex:
/(?:loadNewsItemWithIndex\()[0-9]+/)
Applied like so:
subject.match(/(?:loadNewsItemWithIndex\()[0-9]+/)
The result is:
loadNewsItemWithIndex(5
What is cleanest, most readable way to extract 5 as a one-liner? Is it possible to do this by excluding loadNewsItemWithIndex( from the match rather than matching 5 as a sub group?
The return value from String.match is an array of matches, so you can put parentheses around the number part and just retrieve that particular match index (where the first match is the entire matched result, and subsequent entries are for each capture group):
var subject = "javascript:loadNewsItemWithIndex(5, null);";
var result = subject.match(/loadNewsItemWithIndex\(([0-9]+)/);
// ^ ^ added parens
document.writeln(result[1]);
// ^ retrieve second match (1 in 0-based indexing)
Sample code: http://jsfiddle.net/LT62w/
Edit: Thanks #Alan for the correction on how non-capturing matches work.
Actually, it's working perfectly. Text that's matched inside a
non-capturing group is still consumed, the same as text that's matched
outside of any group. A capturing group is like a non-capturing group
with extra functionality: in addition to grouping, it allows you to
extract whatever it matches independently of the overall match.
I believe the following regex should work for you:
loadNewsItemWithIndex\(([0-9]+).*$
var test = new RegExp(/loadNewsItemWithIndex\(([0-9]+).*$/);
test.exec('var subject = "javascript:loadNewsItemWithIndex(5, null);";');
The break down of this is
loadNewsItemWithIndex = exactly that
\( = open parentheses
([0-9]+) = Capture the number
.* = Anything after that number
$ = end of the line
This should suffice:
<script>
var subject = "javascript:loadNewsItemWithIndex(5, null);";
number = subject.match(/[0-9]+/);
alert(number);
</script>

Categories

Resources