使用RegEx查找具有通配符的URL

时间:2022-09-01 23:04:28

I'm building a site that is a portfolio of projects. I have some pagination that allows you to go next/previous project.

我正在建立一个项目组合的网站。我有一些页面可以让你进入下一个/上一个项目。

I want to run some animations when coming to a project, but not when browsing between projects.

我想在项目中运行一些动画,而不是在项目之间浏览。

My plan is to use the referrer URL to know if you came to a project from another project, and thus not run the animation. But my RegEx is not good, so I'm having trouble.

我的计划是使用引用URL来了解您是否来自另一个项目,从而不运行动画。但是我的RegEx不太好,所以我有麻烦了。

Here is what I'd like to do (pseudo code)

这是我想做的(伪代码)

var refURL = document.referrer;
if( refURL.search('http://www.example.com/work/digital/*') > 0 ) {
    // Do not run animation
} else {
    // Run animation
}

The important thing, is that "http://www.example.com/work/digital/" should be FALSE, but "http://www.example.com/work/digital/*" should be TRUE.

重要的是,“http://www.example.com/work/digital/”应该是错误的,但是“http://www.example.com/work/digital/*”应该是正确的。

So, what's the RegEx to do that?

那么,RegEx要做什么呢?

Thanks!

谢谢!

4 个解决方案

#1


1  

(.*) matches 0 or more characters whereas (.+) matches 1 or more characters

(.*)匹配0个或多个字符,而(.+)匹配一个或多个字符

Also RegExp in JavaScript does not need to be enclosed, so just type

此外,JavaScript中的RegExp不需要封装,因此只需输入

var exp = /pattern/modifiers

For more details visit: http://www.w3schools.com/jsref/jsref_obj_regexp.asp

更多细节请访问:http://www.w3schools.com/jsref/jsref_obj_regexp.asp

My idea for your problem is to try something like:

我对你的问题的看法是:

var refURL = document.referrer;
if (refURL.search(/^http:\/\/www.example.com\/work\/digital\/(.+)$/i) >= 0) {
    // Do not run the animation
} else {
    // Run the animation
}

#2


2  

I think you are looking for this:-

我想你是在找这个:-

var refURL = document.referrer;
if( refURL.search('http://www.example.com/work/digital/(.*)') > 0 ) {
    // Do not run animation
} else {
    // Run animation
}

In other way, you can use indexOf()

换句话说,可以使用indexOf()

refURL.indexOf("http://www.example.com/work/digital/");

if(refURL.indexOf("http://www.example.com/work/digital/") > -1) {
       // Do Your Stuff
}

#3


0  

var re = /^http:\/\/www.example.com\/work\/digital\/.+$/i;
console.log('http://www.example.com/work/digital/x'.search(re));
console.log('http://www.example.com/work/digital/'.search(re));

#4


0  

can use regex and in javascript

可以使用regex和javascript吗

var searchin = item.toLowerCase();
var str = "*abc*";
str = str.replace(/[*]/g, ".*").toLowerCase().trim();
return new RegExp("^"+ str + "$").test(searchin);

#1


1  

(.*) matches 0 or more characters whereas (.+) matches 1 or more characters

(.*)匹配0个或多个字符,而(.+)匹配一个或多个字符

Also RegExp in JavaScript does not need to be enclosed, so just type

此外,JavaScript中的RegExp不需要封装,因此只需输入

var exp = /pattern/modifiers

For more details visit: http://www.w3schools.com/jsref/jsref_obj_regexp.asp

更多细节请访问:http://www.w3schools.com/jsref/jsref_obj_regexp.asp

My idea for your problem is to try something like:

我对你的问题的看法是:

var refURL = document.referrer;
if (refURL.search(/^http:\/\/www.example.com\/work\/digital\/(.+)$/i) >= 0) {
    // Do not run the animation
} else {
    // Run the animation
}

#2


2  

I think you are looking for this:-

我想你是在找这个:-

var refURL = document.referrer;
if( refURL.search('http://www.example.com/work/digital/(.*)') > 0 ) {
    // Do not run animation
} else {
    // Run animation
}

In other way, you can use indexOf()

换句话说,可以使用indexOf()

refURL.indexOf("http://www.example.com/work/digital/");

if(refURL.indexOf("http://www.example.com/work/digital/") > -1) {
       // Do Your Stuff
}

#3


0  

var re = /^http:\/\/www.example.com\/work\/digital\/.+$/i;
console.log('http://www.example.com/work/digital/x'.search(re));
console.log('http://www.example.com/work/digital/'.search(re));

#4


0  

can use regex and in javascript

可以使用regex和javascript吗

var searchin = item.toLowerCase();
var str = "*abc*";
str = str.replace(/[*]/g, ".*").toLowerCase().trim();
return new RegExp("^"+ str + "$").test(searchin);