admin管理员组文章数量:1023909
I've got a question about acplishing something in regular expressions i.e is it possible and if so how.
Short synopsis of what I am doing: I read Manga online. I like to open up the current issue in a new tab but I don't always remember to click the link with a middle click.
So I decided to write a Greasemonkey script that would run on any of the sites pages adding a target="_blank"
to any anchor tag that references a manga or an issue of that manga so a regular click would open it in a new tab. The script itself is working fine, nothing spectacular about it.
Now onto the caveat:
The link patterns we'll call it for each Manga is as follows
<language of manga>/<manga name>
links to the main about page for the manga
<language of manga>/<manga name>/<issue number>
links to the issue number itself
<language of manga>/<manga name>/<issue number>/<page number>
links to the specific page of the issue
Now I want it to always make the first two open in a new tab but I do NOT want the last one to open in a new tab. The reason for this is when reading the actual Manga each click to advance the page would open a new tab.
So to acplish this I have two regular expressions:
var MangaIssueRegex = /en-manga\/[A-Za-z0-9\-]+\//;
var MangaIssuePageRegex = /en-manga\/[A-Za-z0-9\-]+\/(\d+\/){2}/
The first regex picks up any of the three manga link patterns. The second regex picks up only the third manga link pattern (the one for a specific page)
In a nutshell the Greasemonkey script loops through all the anchor tags on the page when it loads and if the href attribute of the anchor tag passes the MangaIssueRegex AND fails the MangaIssuePageRegex the anchor tag is modified to open in a new tab otherwise the anchor tag is not modified.
Now is it possible to bine both regular expressions into a single one that will match the first two patterns but fail if the third pattern is encountered?
Here is the whole script:
var MangaIssueRegex = /en-manga\/[A-Za-z0-9\-]+\//;
var MangaIssuePageRegex = /en-manga\/[A-Za-z0-9\-]+\/(\d+\/){2}/
var elements = document.getElementsByTagName("a");
for (var i = 0; i < elements.length; i++)
{
if (MangaIssueRegex.test(elements[i].getAttribute("href")) && !MangaIssuePageRegex.test(elements[i].getAttribute("href")))
{
elements[i].setAttribute("target","_blank");
elements[i].setAttribute("title","Opens in another tab");
}
}
Update
Thanks to both @DanielHilgarth and @iMoses for helping me figure this out
I found out I can use jQuery in Greasemonkey so in the end this is what I ended up with as the plete working script
$("a")
.filter(function(){return /en-manga\/[\w\-]+\/(\d+\/)?$/.test(this.href);})
.each(function(index){$(this).attr({title:"Opens in new tab",target:"_blank"});});
I've got a question about acplishing something in regular expressions i.e is it possible and if so how.
Short synopsis of what I am doing: I read Manga online. I like to open up the current issue in a new tab but I don't always remember to click the link with a middle click.
So I decided to write a Greasemonkey script that would run on any of the sites pages adding a target="_blank"
to any anchor tag that references a manga or an issue of that manga so a regular click would open it in a new tab. The script itself is working fine, nothing spectacular about it.
Now onto the caveat:
The link patterns we'll call it for each Manga is as follows
<language of manga>/<manga name>
links to the main about page for the manga
<language of manga>/<manga name>/<issue number>
links to the issue number itself
<language of manga>/<manga name>/<issue number>/<page number>
links to the specific page of the issue
Now I want it to always make the first two open in a new tab but I do NOT want the last one to open in a new tab. The reason for this is when reading the actual Manga each click to advance the page would open a new tab.
So to acplish this I have two regular expressions:
var MangaIssueRegex = /en-manga\/[A-Za-z0-9\-]+\//;
var MangaIssuePageRegex = /en-manga\/[A-Za-z0-9\-]+\/(\d+\/){2}/
The first regex picks up any of the three manga link patterns. The second regex picks up only the third manga link pattern (the one for a specific page)
In a nutshell the Greasemonkey script loops through all the anchor tags on the page when it loads and if the href attribute of the anchor tag passes the MangaIssueRegex AND fails the MangaIssuePageRegex the anchor tag is modified to open in a new tab otherwise the anchor tag is not modified.
Now is it possible to bine both regular expressions into a single one that will match the first two patterns but fail if the third pattern is encountered?
Here is the whole script:
var MangaIssueRegex = /en-manga\/[A-Za-z0-9\-]+\//;
var MangaIssuePageRegex = /en-manga\/[A-Za-z0-9\-]+\/(\d+\/){2}/
var elements = document.getElementsByTagName("a");
for (var i = 0; i < elements.length; i++)
{
if (MangaIssueRegex.test(elements[i].getAttribute("href")) && !MangaIssuePageRegex.test(elements[i].getAttribute("href")))
{
elements[i].setAttribute("target","_blank");
elements[i].setAttribute("title","Opens in another tab");
}
}
Update
Thanks to both @DanielHilgarth and @iMoses for helping me figure this out
I found out I can use jQuery in Greasemonkey so in the end this is what I ended up with as the plete working script
$("a")
.filter(function(){return /en-manga\/[\w\-]+\/(\d+\/)?$/.test(this.href);})
.each(function(index){$(this).attr({title:"Opens in new tab",target:"_blank"});});
Share
Improve this question
edited Oct 30, 2017 at 15:32
halfer
20.4k19 gold badges109 silver badges202 bronze badges
asked Mar 21, 2012 at 8:46
TofuBugTofuBug
6031 gold badge7 silver badges22 bronze badges
3 Answers
Reset to default 3For matching only the first two patterns try using:
var MangaIssuePageRegex = /en-manga\/[A-Za-z0-9\-]+\/(\d+\/)?$/
This will match:
<language of manga>/<manga name>
As well as:
<language of manga>/<manga name>/<issue number>
If you don't wish to enforce a closing backslash, use this Regex instead:
var MangaIssuePageRegex = /en-manga\/[A-Za-z0-9\-]+(\/\d+)?(\/)?$/
Can't you just add the text end anchor at the end?
Something like this:
/en-manga\/([A-Za-z0-9\-]+\/){1,2}$/
This would match
en-manga/asd/
en-manga/asd/12/
But not
en-manga/asd/12/123/
You can test it yourself.
Looks like you want to match the first regex when it ends, in which case try changing it to:
var MangaIssueRegex = /en-manga\/[A-Za-z0-9\-]+\/?$/
http://jsfiddle/ZTGuB/1/
I've got a question about acplishing something in regular expressions i.e is it possible and if so how.
Short synopsis of what I am doing: I read Manga online. I like to open up the current issue in a new tab but I don't always remember to click the link with a middle click.
So I decided to write a Greasemonkey script that would run on any of the sites pages adding a target="_blank"
to any anchor tag that references a manga or an issue of that manga so a regular click would open it in a new tab. The script itself is working fine, nothing spectacular about it.
Now onto the caveat:
The link patterns we'll call it for each Manga is as follows
<language of manga>/<manga name>
links to the main about page for the manga
<language of manga>/<manga name>/<issue number>
links to the issue number itself
<language of manga>/<manga name>/<issue number>/<page number>
links to the specific page of the issue
Now I want it to always make the first two open in a new tab but I do NOT want the last one to open in a new tab. The reason for this is when reading the actual Manga each click to advance the page would open a new tab.
So to acplish this I have two regular expressions:
var MangaIssueRegex = /en-manga\/[A-Za-z0-9\-]+\//;
var MangaIssuePageRegex = /en-manga\/[A-Za-z0-9\-]+\/(\d+\/){2}/
The first regex picks up any of the three manga link patterns. The second regex picks up only the third manga link pattern (the one for a specific page)
In a nutshell the Greasemonkey script loops through all the anchor tags on the page when it loads and if the href attribute of the anchor tag passes the MangaIssueRegex AND fails the MangaIssuePageRegex the anchor tag is modified to open in a new tab otherwise the anchor tag is not modified.
Now is it possible to bine both regular expressions into a single one that will match the first two patterns but fail if the third pattern is encountered?
Here is the whole script:
var MangaIssueRegex = /en-manga\/[A-Za-z0-9\-]+\//;
var MangaIssuePageRegex = /en-manga\/[A-Za-z0-9\-]+\/(\d+\/){2}/
var elements = document.getElementsByTagName("a");
for (var i = 0; i < elements.length; i++)
{
if (MangaIssueRegex.test(elements[i].getAttribute("href")) && !MangaIssuePageRegex.test(elements[i].getAttribute("href")))
{
elements[i].setAttribute("target","_blank");
elements[i].setAttribute("title","Opens in another tab");
}
}
Update
Thanks to both @DanielHilgarth and @iMoses for helping me figure this out
I found out I can use jQuery in Greasemonkey so in the end this is what I ended up with as the plete working script
$("a")
.filter(function(){return /en-manga\/[\w\-]+\/(\d+\/)?$/.test(this.href);})
.each(function(index){$(this).attr({title:"Opens in new tab",target:"_blank"});});
I've got a question about acplishing something in regular expressions i.e is it possible and if so how.
Short synopsis of what I am doing: I read Manga online. I like to open up the current issue in a new tab but I don't always remember to click the link with a middle click.
So I decided to write a Greasemonkey script that would run on any of the sites pages adding a target="_blank"
to any anchor tag that references a manga or an issue of that manga so a regular click would open it in a new tab. The script itself is working fine, nothing spectacular about it.
Now onto the caveat:
The link patterns we'll call it for each Manga is as follows
<language of manga>/<manga name>
links to the main about page for the manga
<language of manga>/<manga name>/<issue number>
links to the issue number itself
<language of manga>/<manga name>/<issue number>/<page number>
links to the specific page of the issue
Now I want it to always make the first two open in a new tab but I do NOT want the last one to open in a new tab. The reason for this is when reading the actual Manga each click to advance the page would open a new tab.
So to acplish this I have two regular expressions:
var MangaIssueRegex = /en-manga\/[A-Za-z0-9\-]+\//;
var MangaIssuePageRegex = /en-manga\/[A-Za-z0-9\-]+\/(\d+\/){2}/
The first regex picks up any of the three manga link patterns. The second regex picks up only the third manga link pattern (the one for a specific page)
In a nutshell the Greasemonkey script loops through all the anchor tags on the page when it loads and if the href attribute of the anchor tag passes the MangaIssueRegex AND fails the MangaIssuePageRegex the anchor tag is modified to open in a new tab otherwise the anchor tag is not modified.
Now is it possible to bine both regular expressions into a single one that will match the first two patterns but fail if the third pattern is encountered?
Here is the whole script:
var MangaIssueRegex = /en-manga\/[A-Za-z0-9\-]+\//;
var MangaIssuePageRegex = /en-manga\/[A-Za-z0-9\-]+\/(\d+\/){2}/
var elements = document.getElementsByTagName("a");
for (var i = 0; i < elements.length; i++)
{
if (MangaIssueRegex.test(elements[i].getAttribute("href")) && !MangaIssuePageRegex.test(elements[i].getAttribute("href")))
{
elements[i].setAttribute("target","_blank");
elements[i].setAttribute("title","Opens in another tab");
}
}
Update
Thanks to both @DanielHilgarth and @iMoses for helping me figure this out
I found out I can use jQuery in Greasemonkey so in the end this is what I ended up with as the plete working script
$("a")
.filter(function(){return /en-manga\/[\w\-]+\/(\d+\/)?$/.test(this.href);})
.each(function(index){$(this).attr({title:"Opens in new tab",target:"_blank"});});
Share
Improve this question
edited Oct 30, 2017 at 15:32
halfer
20.4k19 gold badges109 silver badges202 bronze badges
asked Mar 21, 2012 at 8:46
TofuBugTofuBug
6031 gold badge7 silver badges22 bronze badges
3 Answers
Reset to default 3For matching only the first two patterns try using:
var MangaIssuePageRegex = /en-manga\/[A-Za-z0-9\-]+\/(\d+\/)?$/
This will match:
<language of manga>/<manga name>
As well as:
<language of manga>/<manga name>/<issue number>
If you don't wish to enforce a closing backslash, use this Regex instead:
var MangaIssuePageRegex = /en-manga\/[A-Za-z0-9\-]+(\/\d+)?(\/)?$/
Can't you just add the text end anchor at the end?
Something like this:
/en-manga\/([A-Za-z0-9\-]+\/){1,2}$/
This would match
en-manga/asd/
en-manga/asd/12/
But not
en-manga/asd/12/123/
You can test it yourself.
Looks like you want to match the first regex when it ends, in which case try changing it to:
var MangaIssueRegex = /en-manga\/[A-Za-z0-9\-]+\/?$/
http://jsfiddle/ZTGuB/1/
本文标签: javascriptCombining two regular expressionsStack Overflow
版权声明:本文标题:javascript - Combining two regular expressions - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745598158a2158295.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论