How to "place-hold" regex matches in array using javascript - javascript

If I have a multiple match regex like:
var matches = string.match(/\s*(match1)?\s*(match2)?(match3)?\s*/i);
and if my string that I am testing is like this:
var string = "match1 match3";
is there a way to output this array values:
matches[1] = "match1";
matches[2] = "";
matches[3] = "match3";
Notice: what I would like is for the regex to match the entire thing but "place-hold" in the array the matches it does not find.
Hope this makes sense. Thanks for the help!

There already is a "placeholder". Unmatched groups pop an array index matching the group number with an undefined value. e.g.
var someString = "match2";
var matches = someString.match(/\s*(match1)?\s*(match2)?(match3)?\s*/i);
matches now has
["match2", undefined, "match2", undefined]
Element 0 is the complete regex match and elements 1-3 are the individual groups
So you can do for example...
// check if group1
if (typeof matches[1] != 'undefined')

When you want to compare the string to the regexes, just do an Array join.Something like,
matches[1] = "match1";
matches[2] = "";
matches[3] = "match3";
var mystring = mathches.join("");
The joining character could be anything. You could also do,
var mystring = mathches.join(" ");
EDIT:
Not sure from the problem description, but i think you want the regex to output an array.Something like
text = "First line\nSecond line";
var regex = /(\S+) line\n?/y;
would give,
var match = regex.exec(text);
print(match[1]); // prints "First"
print(regex.lastIndex); // prints 11
More about it here

Related

Extract strings between occurences of a specific character

I'm attempting to extract strings between occurences of a specific character in a larger string.
For example:
The initial string is:
var str = "http://www.google.com?hello?kitty?test";
I want to be able to store all of the substrings between the question marks as their own variables, such as "hello", "kitty" and "test".
How would I target substrings between different indexes of a specific character using either JavaScript or Regular Expressions?
You could split on ? and use slice passing 1 as the parameter value.
That would give you an array with your values. If you want to create separate variables you could for example get the value by its index var1 = parts[0]
var str = "http://www.google.com?hello?kitty?test";
var parts = str.split('?').slice(1);
console.log(parts);
var var1 = parts[0],
var2 = parts[1],
var3 = parts[2];
console.log(var1);
console.log(var2);
console.log(var3);
Quick note: that URL would be invalid. A question mark ? denotes the beginning of a query string and key/value pairs are generally provided in the form key=value and delimited with an ampersand &.
That being said, if this isn't a problem then why not split on the question mark to obtain an array of values?
var split_values = str.split('?');
//result: [ 'http://www.google.com', 'hello', 'kitty', 'test' ]
Then you could simply grab the individual values from the array, skipping the first element.
I believe this will do it:
var components = "http://www.google.com?hello?kitty?test".split("?");
components.slice(1-components.length) // Returns: [ "hello", "kitty", "test" ]
using Regular Expressions
var reg = /\?([^\?]+)/g;
var s = "http://www.google.com?hello?kitty?test";
var results = null;
while( results = reg.exec(s) ){
console.log(results[1]);
}
The general case is to use RegExp:
var regex1 = new RegExp(/\?.*?(?=\?|$)/,'g'); regex1.lastIndex=0;
str.match(regex1)
Note that this will also get you the leading ? in each clause (no look-behind regexp in Javascript).
Alternatively you can use the sticky flag and run it in a loop:
var regex1 = new RegExp(/.*?\?(.*?)(?=\?|$)/,'y'); regex1.lastIndex=0;
while(str.match(regex1)) {...}
You can take the substring starting from the first question mark, then split by question mark
const str = "http://www.google.com?hello?kitty?test";
const matches = str.substring(str.indexOf('?') + 1).split(/\?/g);
console.log(matches);

javascript how match whole words but in certain length limit

for example i have this two strings:
string1:
"hi sir may name is Jone"
string2
"hi may name is Jone"
i have this this regex:
var regex = XRegExp('hi(?:(?!hi|Jone).)*?Jone', 'gs');
will match both of them but i want to modify the regex to match only in limited length of the whole string
i want to match the string two "hi may name is Jone" as had less words length how to do it..
If you want to get the string with the least amount of words that also matches your regex, you could split and use a whitespace as a separator and check the length of the returned array.
As an example with an array of strings, you could create var longestString = ""; which will at the end of the loop contain the shortest matched string.
In the loop, first check if there is a match and if longestString is an empty string. If that is the case then set the variable so you have a match to compare against future possible matches.
var strings = [
"test",
"hi sir may name is Jone",
"hi may name is Jone",
"hi Jone",
"hi may name is Jone test",
"hi i am Jone",
"may name is Jone test",
"hi may name is Jane test test 2"
];
var regex = /hi(?:(?!hi|Jone).)*?Jone/;
var longestString = "";
strings.forEach((str) => {
var match = XRegExp.match(str, regex);
if (match && longestString === "") {
longestString = str;
return;
}
if (match && str.split(" ").length < longestString.split(" ").length) {
longestString = str;
}
});
console.log(longestString);
<script src="https://unpkg.com/xregexp/xregexp-all.js"></script>
If you want to match a limited length of the whole string using only regex, I think you can do something like this:
var index = 15;
var testString1 = "hi may name is Jone";
var testString2 = "hi sir may name is Jone";
testString1.match("^.{1," + index + "}Jone"); // This will match
testString2.match("^.{1," + index + "}Jone"); // This string is longer and will not match
Explanation of the regex ^.{1, n}Jone.
^ : should match the start of the string.
.{1, n}Jone : matches everything between 1 to n until the pattern is fully matched.
In this case we define the n as index so this "limit" can be dynamic.
Hope this helps!

Extract alphanumeric words starting with letter, ignore others

Sounds easy enough, but I am just unable to figure that out.
/([a-z][a-z0-9]+)/gi
Test case looks like this
Correct #123 2baZ #1a2 thisToo $bar andTwo2
I am fighting with those partial matches. Only valid should be: ["Correct", "thisToo", "andTwo2"]. Any others should stay unmatched.
Here is link to tester: http://regex101.com/r/qG7lU9/8
Update:
Here is JS fiddle that works better than tester itself... http://jsfiddle.net/FredyCr/6hsgef82/
You can use lookahead and non-capturing group based regex like this:
(?:^| )([a-z][a-z0-9]+(?= |$))
And use captured group #1 for your matches that gives:
Correct
thisToo
andTwo2
RegEx Demo
Code:
var rx = /(?:^| )([a-z][a-z0-9]+(?= |$))/gi
var str = " Correct #123 2baZ #1a2 thisToo $bar andTwo2";
var matches = [];
while (match = rx.exec(str))
matches.push(match[1]);
console.log(matches);
//=> ["Correct", "thisToo", "andTwo2"]
JsFiddle Demo
Get the matched strings from group index 2.
(?:^| )([a-z][a-z0-9]+)(?: |$)
DEMO
Javascript code would be,
> var re = /(?:^| )([a-z][a-z0-9]+)(?: |$)/gi
undefined
> var str = " Correct #123 2baZ #1a2 thisToo $bar andTwo2";
undefined
> var matches = [];
undefined
> while (match = re.exec(str))
... {
... matches.push(match[1]);
... }
3
> console.log(matches);
[ 'Correct', 'thisToo', 'andTwo2' ]
var regex = /(^|\s)[a-z][a-z0-9]+/gi;
var text = "Correct #123 2baZ #1a2 thisToo $bar andTwo2";
var found;
while ((found = regex.exec(text)) !== null)
console.log(found[0].trim());
Output
Correct
thisToo
andTwo2
Workaround: split and filter the tokens you want:
var rx = /^[a-zA-Z][a-zA-Z0-9]*$/;
var match = str.split(/\s+/).filter(rx.exec.bind(rx));
Example

Using Regex to pull out a part of a string

I can't figure out how to pull out multiple matches from the following example:
This code:
/prefix-(\w+)/g.exec('prefix-firstname prefix-lastname');
returns:
["prefix-firstname", "firstname"]
How do I get it to return:
[
["prefix-firstname", "firstname"],
["prefix-lastname", "lastname"]
]
Or
["prefix-firstname", "firstname", "prefix-lastname", "lastname"]
This will do what you want:
var str="prefix-firstname prefix-lastname";
var out =[];
str.replace(/prefix-(\w+)/g,function(match, Group) {
var row = [match, Group]
out.push(row);
});
Probably a mis-use of .replace, but I don't think you can pass a function to .match...
_Pez
Using a loop:
re = /prefix-(\w+)/g;
str = 'prefix-firstname prefix-lastname';
match = re.exec(str);
while (match != null) {
match = re.exec(str);
}
You get each match one at a time.
Using match:
Here, the regex will have to be a bit different, because you cannot get sub-captures (or I don't know how to do it with multiple matches)...
re = /[^\s-]+(?=\s|$)/g;
str = 'prefix-firstname prefix-lastname';
match = str.match(re);
alert(match);
[^\s-]+ matches all characters except spaces and dashes/hyphens only if they are followed by a space or are at the end of the string, which is a confition imposed by (?=\s|$).
You can find the groups in two steps:
"prefix-firstname prefix-lastname".match(/prefix-\w+/g)
.map(function(s) { return s.match(/prefix-(\w+)/) })

Cannot extract parts of a string

I have a string like this : SPList:6E5F5E0D-0CA4-426C-A523-134BA33369D7?SPWeb:C5DD2ADA-E0C4-4971-961F-233789297FE9:.
Using Javascript, I would like to extract the two IDs (which can be different) : 6E5F5E0D-0CA4-426C-A523-134BA33369D7 and C5DD2ADA-E0C4-4971-961F-233789297FE9.
I'm using this regular expression : ^SPList\:(?:[0-9A-Za-z\-]+)\?SPWeb\:(?:[0-9A-Za-z\-]+)\:$.
I expect this expression to extract into two matching groups the two IDs.
By now, my code is :
var input = "SPList:6E5F5E0D-0CA4-426C-A523-134BA33369D7?SPWeb:C5DD2ADA-E0C4-4971-961F-233789297FE9:";
var myregex = /^SPList\:(?:[0-9A-Za-z\-]+)\?SPWeb\:(?:[0-9A-Za-z\-]+)\:$/g;
var match = input.match(myregex);
var listId = match[0];
var webId = match[1];
However, this is not working as expected. The first match contains the whole string, and the second match is undefined.
What is the proper way to extract my ID's?
Here is a jsfiddle that illustrate my issue.
This should suit your needs:
var regex = /^SPList:([0-9A-F-]+)[?]SPWeb:([0-9A-F-]+):$/g;
var match = regex.exec(input);
var listId = match[1];
var webId = match[2];
I simply replaced the non-capturing groups of your initial regex by capturing groups, and used regex.exec(input) instead of input.match(regex) to get the captured data. Also, since the IDs seem to be hexadecimal values, I used A-F instead of A-Z.
try this:
var myregex = /[^\:]([0-9A-Z\-]+)[^\?|\:]/g;
var match = input.match(myregex);
alert("listID: " + match[1] + "\n" + "webID: " + match[3]);

Categories

Resources