JavaScript: Understanding [match] principle Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern) Data science time! April 2019 and salary with experience Should we burninate the [wrap] tag? The Ask Question Wizard is Live!How do JavaScript closures work?What is the most efficient way to deep clone an object in JavaScript?How do I remove a property from a JavaScript object?Which equals operator (== vs ===) should be used in JavaScript comparisons?How do I include a JavaScript file in another JavaScript file?What does “use strict” do in JavaScript, and what is the reasoning behind it?How to check whether a string contains a substring in JavaScript?Storing Objects in HTML5 localStorageHow do I remove a particular element from an array in JavaScript?For-each over an array in JavaScript?
Can an alien society believe that their star system is the universe?
What is Arya's weapon design?
When a candle burns, why does the top of wick glow if bottom of flame is hottest?
How do I stop a creek from eroding my steep embankment?
Is it ethical to give a final exam after the professor has quit before teaching the remaining chapters of the course?
Why didn't this character "real die" when they blew their stack out in Altered Carbon?
Fundamental Solution of the Pell Equation
Understanding Ceva's Theorem
Compare a given version number in the form major.minor.build.patch and see if one is less than the other
Identifying polygons that intersect with another layer using QGIS?
ListPlot join points by nearest neighbor rather than order
51k Euros annually for a family of 4 in Berlin: Is it enough?
Echoing a tail command produces unexpected output?
English words in a non-english sci-fi novel
How can I make names more distinctive without making them longer?
Why do people hide their license plates in the EU?
How to align text above triangle figure
How to answer "Have you ever been terminated?"
How to bypass password on Windows XP account?
If a contract sometimes uses the wrong name, is it still valid?
Why is "Consequences inflicted." not a sentence?
Okay to merge included columns on otherwise identical indexes?
Do I really need recursive chmod to restrict access to a folder?
Why are both D and D# fitting into my E minor key?
JavaScript: Understanding [match] principle
Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern)
Data science time! April 2019 and salary with experience
Should we burninate the [wrap] tag?
The Ask Question Wizard is Live!How do JavaScript closures work?What is the most efficient way to deep clone an object in JavaScript?How do I remove a property from a JavaScript object?Which equals operator (== vs ===) should be used in JavaScript comparisons?How do I include a JavaScript file in another JavaScript file?What does “use strict” do in JavaScript, and what is the reasoning behind it?How to check whether a string contains a substring in JavaScript?Storing Objects in HTML5 localStorageHow do I remove a particular element from an array in JavaScript?For-each over an array in JavaScript?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
New to coding. I don't understand the principle of [match].
I mean;
var wikipediaDontRedirectRegExp = new RegExp("^https?://([a-zA-Z0-9\-_]+)\.(?:m\.)?wikipedia\.(?:com|org)/wiki/(.*)(?:\?oldformat=true|\?previous=yes)", "i");
match = requestedURL.match(wikipediaDontRedirectRegExp);
if (match && match.length === 3)
lang = match[1];
page = match[2];
returnVal.redirectURL = 'http://' + getFullHostName() + '/' + lang + '/' + page;
returnVal.domain = 'imgur';
returnVal.autoRedirect = false;
returnVal.lang = lang;
return returnVal;
On this code, I don't understand that match[1] or match[2] refers.
For example, on
"^https?://([a-zA-Z0-9-_]+).(?:m.)?wikipedia.(?:com|org)/wiki/(.*)(?:?oldformat=true|?previous=yes)",
"i"
what is match[1], what is match[2] ?
Thanks for your help.
javascript safari-extension
add a comment |
New to coding. I don't understand the principle of [match].
I mean;
var wikipediaDontRedirectRegExp = new RegExp("^https?://([a-zA-Z0-9\-_]+)\.(?:m\.)?wikipedia\.(?:com|org)/wiki/(.*)(?:\?oldformat=true|\?previous=yes)", "i");
match = requestedURL.match(wikipediaDontRedirectRegExp);
if (match && match.length === 3)
lang = match[1];
page = match[2];
returnVal.redirectURL = 'http://' + getFullHostName() + '/' + lang + '/' + page;
returnVal.domain = 'imgur';
returnVal.autoRedirect = false;
returnVal.lang = lang;
return returnVal;
On this code, I don't understand that match[1] or match[2] refers.
For example, on
"^https?://([a-zA-Z0-9-_]+).(?:m.)?wikipedia.(?:com|org)/wiki/(.*)(?:?oldformat=true|?previous=yes)",
"i"
what is match[1], what is match[2] ?
Thanks for your help.
javascript safari-extension
1
Did youconsole.log(match)
? Read e.g. developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…?
– jonrsharpe
Mar 8 at 18:01
add a comment |
New to coding. I don't understand the principle of [match].
I mean;
var wikipediaDontRedirectRegExp = new RegExp("^https?://([a-zA-Z0-9\-_]+)\.(?:m\.)?wikipedia\.(?:com|org)/wiki/(.*)(?:\?oldformat=true|\?previous=yes)", "i");
match = requestedURL.match(wikipediaDontRedirectRegExp);
if (match && match.length === 3)
lang = match[1];
page = match[2];
returnVal.redirectURL = 'http://' + getFullHostName() + '/' + lang + '/' + page;
returnVal.domain = 'imgur';
returnVal.autoRedirect = false;
returnVal.lang = lang;
return returnVal;
On this code, I don't understand that match[1] or match[2] refers.
For example, on
"^https?://([a-zA-Z0-9-_]+).(?:m.)?wikipedia.(?:com|org)/wiki/(.*)(?:?oldformat=true|?previous=yes)",
"i"
what is match[1], what is match[2] ?
Thanks for your help.
javascript safari-extension
New to coding. I don't understand the principle of [match].
I mean;
var wikipediaDontRedirectRegExp = new RegExp("^https?://([a-zA-Z0-9\-_]+)\.(?:m\.)?wikipedia\.(?:com|org)/wiki/(.*)(?:\?oldformat=true|\?previous=yes)", "i");
match = requestedURL.match(wikipediaDontRedirectRegExp);
if (match && match.length === 3)
lang = match[1];
page = match[2];
returnVal.redirectURL = 'http://' + getFullHostName() + '/' + lang + '/' + page;
returnVal.domain = 'imgur';
returnVal.autoRedirect = false;
returnVal.lang = lang;
return returnVal;
On this code, I don't understand that match[1] or match[2] refers.
For example, on
"^https?://([a-zA-Z0-9-_]+).(?:m.)?wikipedia.(?:com|org)/wiki/(.*)(?:?oldformat=true|?previous=yes)",
"i"
what is match[1], what is match[2] ?
Thanks for your help.
javascript safari-extension
javascript safari-extension
edited Mar 8 at 17:56
motthebest
asked Mar 8 at 17:50
motthebestmotthebest
62
62
1
Did youconsole.log(match)
? Read e.g. developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…?
– jonrsharpe
Mar 8 at 18:01
add a comment |
1
Did youconsole.log(match)
? Read e.g. developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…?
– jonrsharpe
Mar 8 at 18:01
1
1
Did you
console.log(match)
? Read e.g. developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…?– jonrsharpe
Mar 8 at 18:01
Did you
console.log(match)
? Read e.g. developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…?– jonrsharpe
Mar 8 at 18:01
add a comment |
3 Answers
3
active
oldest
votes
Match is referring to the second line statement:match = requestedURL.match(wikipediaDontRedirectRegExp);
This is using the match function to create the variable also named match. The returned value of the match function is an array, so the next two lines are grabbing the first two values out of this array and setting them to lang
and page
.
More details on the .match()
function can be found here: https://www.w3schools.com/jsref/jsref_match.asp
Thanks for your comment, but I don't understand.lang = match[1] page = match[2];
I understand that;[a-zA-Z0-9-_]
is match[1], but I don't get the match[2].
– motthebest
Mar 8 at 18:07
The function.match()
returns an array of all matches found using the regular expression.match[2]
is grabbing another match found using the function.
– Tim Hunter
Mar 8 at 18:09
Could you show me the match[2] on this:"^https?://([a-zA-Z0-9-_]+).(?:m.)?wikipedia.(?:com|org)/wiki/(.*)(?:?oldformat=true|?previous=yes)", "i"
?
– motthebest
Mar 8 at 18:11
Ohhhh, you want a breakdown of how the regular expression works then?
– Tim Hunter
Mar 8 at 18:13
I'm a real newbie.
– motthebest
Mar 8 at 18:15
|
show 2 more comments
In your case, you have the expression,
^https?://([a-zA-Z0-9\-_]+)\.(?:m\.)?wikipedia\.(?:com|org)/wiki/(.*)(?:\?oldformat=true|\?previous=yes)
The expression is looking for groups like ([a-zA-Z0-9-_]+) or (.*) that doesn' have a cancel, a quotion mark in front of it such as (?: blabla ).
Match is the results you get from the regular expression in your code. For each match, the elements you get in the array from element 1 to the size of the element are these groups.
For example, if we try to find the character couples that have space in between and 'u' after the couple, we use the code below.
Ex: The quick would return ["e qu", "e", "q"]
.
var paragraph = 'The quick brown fox jumps over the lazy dog. It barked.';
var regex = /(w) (w)u/;
var found = paragraph.match(regex);
console.log(found);
// Would return Array ["e qu", "e", "q"]
In Array ["e qu", "e", "q"]
The first element is the whole match, and the other elements found[1] and found[2] are the groups (the brackets) that I mentioned.
Thanks for your detailed comment. On imgur.com/gallery/BLABLA how can I write the code for take the BLABLA. I mean,var imgurDontRedirectRegExp = new RegExp("^https?://imgur.com)/gallery/(.*)", "i"); match = requestedURL.match(imgurDontRedirectRegExp); if (match && match.length === 2) { page = match[1]; returnVal.redirectURL = 'http://' + getFullHostName() + '/' + page;
I want to redirecting all imgur.com urls to 0imgur.com urls. For this I need to take the characters after /gallery/ to a 'var'.
– motthebest
Mar 8 at 18:57
It is pretty much the same way. Look for more examples of the regular expressions to understand how they work. Here are some websites that might help w3schools.com/Js/js_regexp.asp, eloquentjavascript.net/09_regexp.html. Try it out yourself and post another question if you still have any worries with errors you got.
– Fatih Aktaş
Mar 8 at 19:48
add a comment |
Alright, lemme see if I can do a breakdown on this regular expression for you. I'm far from an expert on using RegEx's but I'm sure there's plenty of people on this site who can point out my shortcomings with this.
Expression:^https?://([a-zA-Z0-9\-_]+)\.(?:m\.)?wikipedia\.(?:com|org)/wiki/(.*)(?:\?oldformat=true|\?previous=yes)
^
character essentially having it check if it's a new line or start of input
http
match should have this in it
s?
match can optionally have "s" at this part
://
match should have this in it
(
begin making a group to save for easy reference later
[a-zA-Z0-9\-_]
match most letters, numbers, and special characters
+
previous defined group should contain one or more characters
)
closing group marker
\.
escape needed to include the .
character in match
(?:
create group but do not have this group saved for reference later
m\.
match should have "m." in it
)
closing group marker
?
previous group made is optional
wikipedia\.
match should have "wikipedia." in it
(?:
create group but do not have this group saved for reference later
com|org
match "com" or "org", they're interchangeable
)
closing group marker
/wiki/
match should have "/wiki/" in it
(
make a group that is saved for reference later
.
match letters, numbers, or special characters
*
include zero or more characters for previous marker
)
closing group marker
(?:
create group but do not save it
\?
include "?" in match, meaning this is going to check queries put on the URL
\?oldformat=true|\?previous=yes
check if "?oldformat=true" or "?previous=yes" queries are attached
)
closing group marker
I think that about sums it up. If you have any more questions on it feel free to comment. But the gist of it is, match[1] is going to grab the ([a-zA-Z0-9\-_]+)
group and the match[2] is going to grab the (.*)
group after the /wiki/
directory portion.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55068470%2fjavascript-understanding-match-principle%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
Match is referring to the second line statement:match = requestedURL.match(wikipediaDontRedirectRegExp);
This is using the match function to create the variable also named match. The returned value of the match function is an array, so the next two lines are grabbing the first two values out of this array and setting them to lang
and page
.
More details on the .match()
function can be found here: https://www.w3schools.com/jsref/jsref_match.asp
Thanks for your comment, but I don't understand.lang = match[1] page = match[2];
I understand that;[a-zA-Z0-9-_]
is match[1], but I don't get the match[2].
– motthebest
Mar 8 at 18:07
The function.match()
returns an array of all matches found using the regular expression.match[2]
is grabbing another match found using the function.
– Tim Hunter
Mar 8 at 18:09
Could you show me the match[2] on this:"^https?://([a-zA-Z0-9-_]+).(?:m.)?wikipedia.(?:com|org)/wiki/(.*)(?:?oldformat=true|?previous=yes)", "i"
?
– motthebest
Mar 8 at 18:11
Ohhhh, you want a breakdown of how the regular expression works then?
– Tim Hunter
Mar 8 at 18:13
I'm a real newbie.
– motthebest
Mar 8 at 18:15
|
show 2 more comments
Match is referring to the second line statement:match = requestedURL.match(wikipediaDontRedirectRegExp);
This is using the match function to create the variable also named match. The returned value of the match function is an array, so the next two lines are grabbing the first two values out of this array and setting them to lang
and page
.
More details on the .match()
function can be found here: https://www.w3schools.com/jsref/jsref_match.asp
Thanks for your comment, but I don't understand.lang = match[1] page = match[2];
I understand that;[a-zA-Z0-9-_]
is match[1], but I don't get the match[2].
– motthebest
Mar 8 at 18:07
The function.match()
returns an array of all matches found using the regular expression.match[2]
is grabbing another match found using the function.
– Tim Hunter
Mar 8 at 18:09
Could you show me the match[2] on this:"^https?://([a-zA-Z0-9-_]+).(?:m.)?wikipedia.(?:com|org)/wiki/(.*)(?:?oldformat=true|?previous=yes)", "i"
?
– motthebest
Mar 8 at 18:11
Ohhhh, you want a breakdown of how the regular expression works then?
– Tim Hunter
Mar 8 at 18:13
I'm a real newbie.
– motthebest
Mar 8 at 18:15
|
show 2 more comments
Match is referring to the second line statement:match = requestedURL.match(wikipediaDontRedirectRegExp);
This is using the match function to create the variable also named match. The returned value of the match function is an array, so the next two lines are grabbing the first two values out of this array and setting them to lang
and page
.
More details on the .match()
function can be found here: https://www.w3schools.com/jsref/jsref_match.asp
Match is referring to the second line statement:match = requestedURL.match(wikipediaDontRedirectRegExp);
This is using the match function to create the variable also named match. The returned value of the match function is an array, so the next two lines are grabbing the first two values out of this array and setting them to lang
and page
.
More details on the .match()
function can be found here: https://www.w3schools.com/jsref/jsref_match.asp
answered Mar 8 at 18:03
Tim HunterTim Hunter
915
915
Thanks for your comment, but I don't understand.lang = match[1] page = match[2];
I understand that;[a-zA-Z0-9-_]
is match[1], but I don't get the match[2].
– motthebest
Mar 8 at 18:07
The function.match()
returns an array of all matches found using the regular expression.match[2]
is grabbing another match found using the function.
– Tim Hunter
Mar 8 at 18:09
Could you show me the match[2] on this:"^https?://([a-zA-Z0-9-_]+).(?:m.)?wikipedia.(?:com|org)/wiki/(.*)(?:?oldformat=true|?previous=yes)", "i"
?
– motthebest
Mar 8 at 18:11
Ohhhh, you want a breakdown of how the regular expression works then?
– Tim Hunter
Mar 8 at 18:13
I'm a real newbie.
– motthebest
Mar 8 at 18:15
|
show 2 more comments
Thanks for your comment, but I don't understand.lang = match[1] page = match[2];
I understand that;[a-zA-Z0-9-_]
is match[1], but I don't get the match[2].
– motthebest
Mar 8 at 18:07
The function.match()
returns an array of all matches found using the regular expression.match[2]
is grabbing another match found using the function.
– Tim Hunter
Mar 8 at 18:09
Could you show me the match[2] on this:"^https?://([a-zA-Z0-9-_]+).(?:m.)?wikipedia.(?:com|org)/wiki/(.*)(?:?oldformat=true|?previous=yes)", "i"
?
– motthebest
Mar 8 at 18:11
Ohhhh, you want a breakdown of how the regular expression works then?
– Tim Hunter
Mar 8 at 18:13
I'm a real newbie.
– motthebest
Mar 8 at 18:15
Thanks for your comment, but I don't understand.
lang = match[1] page = match[2];
I understand that; [a-zA-Z0-9-_]
is match[1], but I don't get the match[2].– motthebest
Mar 8 at 18:07
Thanks for your comment, but I don't understand.
lang = match[1] page = match[2];
I understand that; [a-zA-Z0-9-_]
is match[1], but I don't get the match[2].– motthebest
Mar 8 at 18:07
The function
.match()
returns an array of all matches found using the regular expression. match[2]
is grabbing another match found using the function.– Tim Hunter
Mar 8 at 18:09
The function
.match()
returns an array of all matches found using the regular expression. match[2]
is grabbing another match found using the function.– Tim Hunter
Mar 8 at 18:09
Could you show me the match[2] on this:
"^https?://([a-zA-Z0-9-_]+).(?:m.)?wikipedia.(?:com|org)/wiki/(.*)(?:?oldformat=true|?previous=yes)", "i"
?– motthebest
Mar 8 at 18:11
Could you show me the match[2] on this:
"^https?://([a-zA-Z0-9-_]+).(?:m.)?wikipedia.(?:com|org)/wiki/(.*)(?:?oldformat=true|?previous=yes)", "i"
?– motthebest
Mar 8 at 18:11
Ohhhh, you want a breakdown of how the regular expression works then?
– Tim Hunter
Mar 8 at 18:13
Ohhhh, you want a breakdown of how the regular expression works then?
– Tim Hunter
Mar 8 at 18:13
I'm a real newbie.
– motthebest
Mar 8 at 18:15
I'm a real newbie.
– motthebest
Mar 8 at 18:15
|
show 2 more comments
In your case, you have the expression,
^https?://([a-zA-Z0-9\-_]+)\.(?:m\.)?wikipedia\.(?:com|org)/wiki/(.*)(?:\?oldformat=true|\?previous=yes)
The expression is looking for groups like ([a-zA-Z0-9-_]+) or (.*) that doesn' have a cancel, a quotion mark in front of it such as (?: blabla ).
Match is the results you get from the regular expression in your code. For each match, the elements you get in the array from element 1 to the size of the element are these groups.
For example, if we try to find the character couples that have space in between and 'u' after the couple, we use the code below.
Ex: The quick would return ["e qu", "e", "q"]
.
var paragraph = 'The quick brown fox jumps over the lazy dog. It barked.';
var regex = /(w) (w)u/;
var found = paragraph.match(regex);
console.log(found);
// Would return Array ["e qu", "e", "q"]
In Array ["e qu", "e", "q"]
The first element is the whole match, and the other elements found[1] and found[2] are the groups (the brackets) that I mentioned.
Thanks for your detailed comment. On imgur.com/gallery/BLABLA how can I write the code for take the BLABLA. I mean,var imgurDontRedirectRegExp = new RegExp("^https?://imgur.com)/gallery/(.*)", "i"); match = requestedURL.match(imgurDontRedirectRegExp); if (match && match.length === 2) { page = match[1]; returnVal.redirectURL = 'http://' + getFullHostName() + '/' + page;
I want to redirecting all imgur.com urls to 0imgur.com urls. For this I need to take the characters after /gallery/ to a 'var'.
– motthebest
Mar 8 at 18:57
It is pretty much the same way. Look for more examples of the regular expressions to understand how they work. Here are some websites that might help w3schools.com/Js/js_regexp.asp, eloquentjavascript.net/09_regexp.html. Try it out yourself and post another question if you still have any worries with errors you got.
– Fatih Aktaş
Mar 8 at 19:48
add a comment |
In your case, you have the expression,
^https?://([a-zA-Z0-9\-_]+)\.(?:m\.)?wikipedia\.(?:com|org)/wiki/(.*)(?:\?oldformat=true|\?previous=yes)
The expression is looking for groups like ([a-zA-Z0-9-_]+) or (.*) that doesn' have a cancel, a quotion mark in front of it such as (?: blabla ).
Match is the results you get from the regular expression in your code. For each match, the elements you get in the array from element 1 to the size of the element are these groups.
For example, if we try to find the character couples that have space in between and 'u' after the couple, we use the code below.
Ex: The quick would return ["e qu", "e", "q"]
.
var paragraph = 'The quick brown fox jumps over the lazy dog. It barked.';
var regex = /(w) (w)u/;
var found = paragraph.match(regex);
console.log(found);
// Would return Array ["e qu", "e", "q"]
In Array ["e qu", "e", "q"]
The first element is the whole match, and the other elements found[1] and found[2] are the groups (the brackets) that I mentioned.
Thanks for your detailed comment. On imgur.com/gallery/BLABLA how can I write the code for take the BLABLA. I mean,var imgurDontRedirectRegExp = new RegExp("^https?://imgur.com)/gallery/(.*)", "i"); match = requestedURL.match(imgurDontRedirectRegExp); if (match && match.length === 2) { page = match[1]; returnVal.redirectURL = 'http://' + getFullHostName() + '/' + page;
I want to redirecting all imgur.com urls to 0imgur.com urls. For this I need to take the characters after /gallery/ to a 'var'.
– motthebest
Mar 8 at 18:57
It is pretty much the same way. Look for more examples of the regular expressions to understand how they work. Here are some websites that might help w3schools.com/Js/js_regexp.asp, eloquentjavascript.net/09_regexp.html. Try it out yourself and post another question if you still have any worries with errors you got.
– Fatih Aktaş
Mar 8 at 19:48
add a comment |
In your case, you have the expression,
^https?://([a-zA-Z0-9\-_]+)\.(?:m\.)?wikipedia\.(?:com|org)/wiki/(.*)(?:\?oldformat=true|\?previous=yes)
The expression is looking for groups like ([a-zA-Z0-9-_]+) or (.*) that doesn' have a cancel, a quotion mark in front of it such as (?: blabla ).
Match is the results you get from the regular expression in your code. For each match, the elements you get in the array from element 1 to the size of the element are these groups.
For example, if we try to find the character couples that have space in between and 'u' after the couple, we use the code below.
Ex: The quick would return ["e qu", "e", "q"]
.
var paragraph = 'The quick brown fox jumps over the lazy dog. It barked.';
var regex = /(w) (w)u/;
var found = paragraph.match(regex);
console.log(found);
// Would return Array ["e qu", "e", "q"]
In Array ["e qu", "e", "q"]
The first element is the whole match, and the other elements found[1] and found[2] are the groups (the brackets) that I mentioned.
In your case, you have the expression,
^https?://([a-zA-Z0-9\-_]+)\.(?:m\.)?wikipedia\.(?:com|org)/wiki/(.*)(?:\?oldformat=true|\?previous=yes)
The expression is looking for groups like ([a-zA-Z0-9-_]+) or (.*) that doesn' have a cancel, a quotion mark in front of it such as (?: blabla ).
Match is the results you get from the regular expression in your code. For each match, the elements you get in the array from element 1 to the size of the element are these groups.
For example, if we try to find the character couples that have space in between and 'u' after the couple, we use the code below.
Ex: The quick would return ["e qu", "e", "q"]
.
var paragraph = 'The quick brown fox jumps over the lazy dog. It barked.';
var regex = /(w) (w)u/;
var found = paragraph.match(regex);
console.log(found);
// Would return Array ["e qu", "e", "q"]
In Array ["e qu", "e", "q"]
The first element is the whole match, and the other elements found[1] and found[2] are the groups (the brackets) that I mentioned.
edited Mar 8 at 18:21
answered Mar 8 at 18:14
Fatih AktaşFatih Aktaş
430313
430313
Thanks for your detailed comment. On imgur.com/gallery/BLABLA how can I write the code for take the BLABLA. I mean,var imgurDontRedirectRegExp = new RegExp("^https?://imgur.com)/gallery/(.*)", "i"); match = requestedURL.match(imgurDontRedirectRegExp); if (match && match.length === 2) { page = match[1]; returnVal.redirectURL = 'http://' + getFullHostName() + '/' + page;
I want to redirecting all imgur.com urls to 0imgur.com urls. For this I need to take the characters after /gallery/ to a 'var'.
– motthebest
Mar 8 at 18:57
It is pretty much the same way. Look for more examples of the regular expressions to understand how they work. Here are some websites that might help w3schools.com/Js/js_regexp.asp, eloquentjavascript.net/09_regexp.html. Try it out yourself and post another question if you still have any worries with errors you got.
– Fatih Aktaş
Mar 8 at 19:48
add a comment |
Thanks for your detailed comment. On imgur.com/gallery/BLABLA how can I write the code for take the BLABLA. I mean,var imgurDontRedirectRegExp = new RegExp("^https?://imgur.com)/gallery/(.*)", "i"); match = requestedURL.match(imgurDontRedirectRegExp); if (match && match.length === 2) { page = match[1]; returnVal.redirectURL = 'http://' + getFullHostName() + '/' + page;
I want to redirecting all imgur.com urls to 0imgur.com urls. For this I need to take the characters after /gallery/ to a 'var'.
– motthebest
Mar 8 at 18:57
It is pretty much the same way. Look for more examples of the regular expressions to understand how they work. Here are some websites that might help w3schools.com/Js/js_regexp.asp, eloquentjavascript.net/09_regexp.html. Try it out yourself and post another question if you still have any worries with errors you got.
– Fatih Aktaş
Mar 8 at 19:48
Thanks for your detailed comment. On imgur.com/gallery/BLABLA how can I write the code for take the BLABLA. I mean,
var imgurDontRedirectRegExp = new RegExp("^https?://imgur.com)/gallery/(.*)", "i"); match = requestedURL.match(imgurDontRedirectRegExp); if (match && match.length === 2) { page = match[1]; returnVal.redirectURL = 'http://' + getFullHostName() + '/' + page;
I want to redirecting all imgur.com urls to 0imgur.com urls. For this I need to take the characters after /gallery/ to a 'var'.– motthebest
Mar 8 at 18:57
Thanks for your detailed comment. On imgur.com/gallery/BLABLA how can I write the code for take the BLABLA. I mean,
var imgurDontRedirectRegExp = new RegExp("^https?://imgur.com)/gallery/(.*)", "i"); match = requestedURL.match(imgurDontRedirectRegExp); if (match && match.length === 2) { page = match[1]; returnVal.redirectURL = 'http://' + getFullHostName() + '/' + page;
I want to redirecting all imgur.com urls to 0imgur.com urls. For this I need to take the characters after /gallery/ to a 'var'.– motthebest
Mar 8 at 18:57
It is pretty much the same way. Look for more examples of the regular expressions to understand how they work. Here are some websites that might help w3schools.com/Js/js_regexp.asp, eloquentjavascript.net/09_regexp.html. Try it out yourself and post another question if you still have any worries with errors you got.
– Fatih Aktaş
Mar 8 at 19:48
It is pretty much the same way. Look for more examples of the regular expressions to understand how they work. Here are some websites that might help w3schools.com/Js/js_regexp.asp, eloquentjavascript.net/09_regexp.html. Try it out yourself and post another question if you still have any worries with errors you got.
– Fatih Aktaş
Mar 8 at 19:48
add a comment |
Alright, lemme see if I can do a breakdown on this regular expression for you. I'm far from an expert on using RegEx's but I'm sure there's plenty of people on this site who can point out my shortcomings with this.
Expression:^https?://([a-zA-Z0-9\-_]+)\.(?:m\.)?wikipedia\.(?:com|org)/wiki/(.*)(?:\?oldformat=true|\?previous=yes)
^
character essentially having it check if it's a new line or start of input
http
match should have this in it
s?
match can optionally have "s" at this part
://
match should have this in it
(
begin making a group to save for easy reference later
[a-zA-Z0-9\-_]
match most letters, numbers, and special characters
+
previous defined group should contain one or more characters
)
closing group marker
\.
escape needed to include the .
character in match
(?:
create group but do not have this group saved for reference later
m\.
match should have "m." in it
)
closing group marker
?
previous group made is optional
wikipedia\.
match should have "wikipedia." in it
(?:
create group but do not have this group saved for reference later
com|org
match "com" or "org", they're interchangeable
)
closing group marker
/wiki/
match should have "/wiki/" in it
(
make a group that is saved for reference later
.
match letters, numbers, or special characters
*
include zero or more characters for previous marker
)
closing group marker
(?:
create group but do not save it
\?
include "?" in match, meaning this is going to check queries put on the URL
\?oldformat=true|\?previous=yes
check if "?oldformat=true" or "?previous=yes" queries are attached
)
closing group marker
I think that about sums it up. If you have any more questions on it feel free to comment. But the gist of it is, match[1] is going to grab the ([a-zA-Z0-9\-_]+)
group and the match[2] is going to grab the (.*)
group after the /wiki/
directory portion.
add a comment |
Alright, lemme see if I can do a breakdown on this regular expression for you. I'm far from an expert on using RegEx's but I'm sure there's plenty of people on this site who can point out my shortcomings with this.
Expression:^https?://([a-zA-Z0-9\-_]+)\.(?:m\.)?wikipedia\.(?:com|org)/wiki/(.*)(?:\?oldformat=true|\?previous=yes)
^
character essentially having it check if it's a new line or start of input
http
match should have this in it
s?
match can optionally have "s" at this part
://
match should have this in it
(
begin making a group to save for easy reference later
[a-zA-Z0-9\-_]
match most letters, numbers, and special characters
+
previous defined group should contain one or more characters
)
closing group marker
\.
escape needed to include the .
character in match
(?:
create group but do not have this group saved for reference later
m\.
match should have "m." in it
)
closing group marker
?
previous group made is optional
wikipedia\.
match should have "wikipedia." in it
(?:
create group but do not have this group saved for reference later
com|org
match "com" or "org", they're interchangeable
)
closing group marker
/wiki/
match should have "/wiki/" in it
(
make a group that is saved for reference later
.
match letters, numbers, or special characters
*
include zero or more characters for previous marker
)
closing group marker
(?:
create group but do not save it
\?
include "?" in match, meaning this is going to check queries put on the URL
\?oldformat=true|\?previous=yes
check if "?oldformat=true" or "?previous=yes" queries are attached
)
closing group marker
I think that about sums it up. If you have any more questions on it feel free to comment. But the gist of it is, match[1] is going to grab the ([a-zA-Z0-9\-_]+)
group and the match[2] is going to grab the (.*)
group after the /wiki/
directory portion.
add a comment |
Alright, lemme see if I can do a breakdown on this regular expression for you. I'm far from an expert on using RegEx's but I'm sure there's plenty of people on this site who can point out my shortcomings with this.
Expression:^https?://([a-zA-Z0-9\-_]+)\.(?:m\.)?wikipedia\.(?:com|org)/wiki/(.*)(?:\?oldformat=true|\?previous=yes)
^
character essentially having it check if it's a new line or start of input
http
match should have this in it
s?
match can optionally have "s" at this part
://
match should have this in it
(
begin making a group to save for easy reference later
[a-zA-Z0-9\-_]
match most letters, numbers, and special characters
+
previous defined group should contain one or more characters
)
closing group marker
\.
escape needed to include the .
character in match
(?:
create group but do not have this group saved for reference later
m\.
match should have "m." in it
)
closing group marker
?
previous group made is optional
wikipedia\.
match should have "wikipedia." in it
(?:
create group but do not have this group saved for reference later
com|org
match "com" or "org", they're interchangeable
)
closing group marker
/wiki/
match should have "/wiki/" in it
(
make a group that is saved for reference later
.
match letters, numbers, or special characters
*
include zero or more characters for previous marker
)
closing group marker
(?:
create group but do not save it
\?
include "?" in match, meaning this is going to check queries put on the URL
\?oldformat=true|\?previous=yes
check if "?oldformat=true" or "?previous=yes" queries are attached
)
closing group marker
I think that about sums it up. If you have any more questions on it feel free to comment. But the gist of it is, match[1] is going to grab the ([a-zA-Z0-9\-_]+)
group and the match[2] is going to grab the (.*)
group after the /wiki/
directory portion.
Alright, lemme see if I can do a breakdown on this regular expression for you. I'm far from an expert on using RegEx's but I'm sure there's plenty of people on this site who can point out my shortcomings with this.
Expression:^https?://([a-zA-Z0-9\-_]+)\.(?:m\.)?wikipedia\.(?:com|org)/wiki/(.*)(?:\?oldformat=true|\?previous=yes)
^
character essentially having it check if it's a new line or start of input
http
match should have this in it
s?
match can optionally have "s" at this part
://
match should have this in it
(
begin making a group to save for easy reference later
[a-zA-Z0-9\-_]
match most letters, numbers, and special characters
+
previous defined group should contain one or more characters
)
closing group marker
\.
escape needed to include the .
character in match
(?:
create group but do not have this group saved for reference later
m\.
match should have "m." in it
)
closing group marker
?
previous group made is optional
wikipedia\.
match should have "wikipedia." in it
(?:
create group but do not have this group saved for reference later
com|org
match "com" or "org", they're interchangeable
)
closing group marker
/wiki/
match should have "/wiki/" in it
(
make a group that is saved for reference later
.
match letters, numbers, or special characters
*
include zero or more characters for previous marker
)
closing group marker
(?:
create group but do not save it
\?
include "?" in match, meaning this is going to check queries put on the URL
\?oldformat=true|\?previous=yes
check if "?oldformat=true" or "?previous=yes" queries are attached
)
closing group marker
I think that about sums it up. If you have any more questions on it feel free to comment. But the gist of it is, match[1] is going to grab the ([a-zA-Z0-9\-_]+)
group and the match[2] is going to grab the (.*)
group after the /wiki/
directory portion.
edited Mar 8 at 19:31
answered Mar 8 at 19:25
Tim HunterTim Hunter
915
915
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55068470%2fjavascript-understanding-match-principle%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Did you
console.log(match)
? Read e.g. developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…?– jonrsharpe
Mar 8 at 18:01