Im trying to match the following string:
http://*/*/checkout
to this URL:
http://www.url.com/sub-folder/checkout
Ultimately what i am trying to do is to find a way to display my JavaScript widget on certain pages by allowing to add conditions like the above.
How could i use the string to see if the current URL matches?
Use a regex:
> /^http:\/\/.*?\/.*?\/checkout$/.test('http://www.url.com/sub-folder/checkout')
true
Here's a more readable version:
RegExp('^http://.*?/.*?/checkout$').test('http://www.url.com/sub-folder/checkout')
Related
I am using cookieconsent.js to show a popup for users to accept for my website. I need to stop the cookie consent popup from showing if a page has a certain query string.
The documentation for cookieconsent provides a solution to "blacklistPage" where I can "Specify pages using string or RegExp" that I want to prevent the popup from showing on.
This is fine until I try to use regex for a query string.
Example of path, filename and query string to match:
/sub-folder/file-name.shtml?value=pair
"blacklistPage": [
"/.*\?value=pair"
]
According to the documentation it's expecting either regex or a string but you're trying to pass regex in a string which isn't valid.
using a string : ‘/index.html’ (matches ‘/index.html’ exactly)
using RegExp : /\/page_[\d]+.html/ (matched ‘/page_1.html’ and ‘/page_2.html’ etc)
Additionally you're quoting the blacklistPage, this doesn't need to be quoted.
By removing the quotes and provide a standard JS regex format you can make the following:
blacklistPage: [
/\/.*\?value=pair/
]
Alternatively your use case is simple so you could just use a string and avoid regex:
blacklistPage: [
'/sub-folder/file-name.shtml?value=pair'
]
I have come to the conclusion, along with a friend who knows js much better than I, that the cookieconsent.js script will not allow query strings.
This might sound weird, but i want to parse a xml response by pushing certain string with following regular expression:
data-href="[\d\w\/:\.\=]*[">]?
into an array.
The reason for this is just for testing. A friend has built a webpage with jimdo where he displays a image gallery. now i want to try parsing the xml on this site and only fetch the images which are at every data-href tag and use them in my react native app.
Any ideas?
Why not just use the String.match method?
var matches = xml_str.match(/data-href="[\d\w\/:\.\=]*[">]?/g);
Edit: if you want to just get the URL within the href, use regex capture groups:
var matches = xml_str.match(/data-href="([\d\w\/:\.\=]*)[">]?/g);
console.log(matches);
You can then map the matches array to only contain the element with the URL.
I'm trying to set up a ShareX custom engine, and after the upload I'm given the full url, for instance http://foo.com/HF139hR and I can work that string with regex before copying it to clipboard. What I want to do is to get only the last part of the url, HF139hR so I can throw it into another url, say http://foo.com/?viewer=HF139hR. So far I was using the expression\w+$ to grab it but sometimes I can get an upload error, and that will also get the last word of the error message and pass it to ?viewer=.
Doing my research I found \bfoo.com\/\K\S+, which is exactly what I want, but unfortunately it is not supported on javascript.
\bfoo.com/\K\S+
\bfoo.com\/(\S+)
You can use a similar one and grab the group 1 or capture 1
You can use this Regex: /\/(\w+)(\?+.*)*$/ and get the capturing group between (), this will avoid the part of the upload error which starts with ? like in the example `?viewer=$1$, you can try it here:
var url="http://foo.com/HF139hR?=viewer=$1$";
var reg=/\/(\w+)(\?+.*)*$/;
alert(url.match(reg)[1]);
And if you use only the url="http://foo.com/HF139hR" as a url it will also match the same thing.
And you can take a look at this Regex DEMO where you can see the match information.
I am trying to replace a two multiline comments (on a single line) with javascript text in the middle. I am using a build tool, which reads the entire file, and need to replace a specific string (made up of comments) during the build.
Example:
var data = /*testThisDelete:start*/new Date();/*testThisDelete:end*/
Once replaced, should used like this
var data = 4.6.88
Try something like this to get started:
"your file as a string".replace(new RegExp('/\*testThisDelete\:start.*testThisDelete\:end\*/','m'), '"replacement text"');
See this post for a lot of useful additional info: JavaScript replace/regex
Are you looking for:
^.+?(\/\*testThisDelete:start\*\/.+?\/\*testThisDelete:end\*\/)$
With this you should just be able to replace the first matched substring with what you want.
Assume I have the following URL stored in variable called content:
http://www.example.com/watch?v=4444444&feature=related
Problem:
I need to replace watch?v= with embed/
I need to erase whatever comes after &
The final output would look like:
http://www.example.com/embed/4444444
I tried these two steps but didn't work:
content = content.replace('/watch?v=/', 'embed/');
content = content.replace('&*/g','');
The URL in page source code appears as:
http://www.example.com/watch?v=4444444&feature=related
You have many errors:
You are using a regular expression when you only need a string.
You are writing your regular expressions as strings.
To write 'match any characters' you need to write '.*', not just '*'. The star modifies the previous token.
There is no need to use the g flag here.
Try this instead:
content = content.replace('watch?v=', 'embed/').replace(/&.*/, '');