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.
Related
What regex would I need to get <t:1583277337:R> to then replace it so it display the proper information (in this case: 3 Years ago)
Why?
I'm trying to use it on Discord where Discord automatically converts <t:1583277337:R> to the corresponding data, but the API retrieved data (below) doesnt automatically convert on Discord Embeds
Data
"bio": "𝖓𝖔𝖙 𝖊𝖛𝖊𝖗𝖞𝖔𝖓𝖊 𝖜𝖎𝖑𝖑 𝖇𝖊𝖑𝖎𝖊𝖛𝖊 𝖎𝖓 𝖞𝖔𝖚, 𝖇𝖚𝖙 𝖓𝖔𝖙 𝖊𝖛𝖊𝖗𝖞𝖔𝖓𝖊 𝖒𝖆𝖙𝖙𝖊𝖗𝖘...\n\n\n<t:1583277337:R> <3"
I won't give you the full solution, but here is the logic:
explicit regex match for an <t:, in sequence
digits of any amount
explicit regex match for :R>
You can use this regex in String#replace. The replace method can accept a callback as its second argument for a more powerful parsing of a match. In this case, you can process the string in the replace callback to whatever text you need
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace#parameters
Example with group name:
<t:(?<timestamp>\d*):R>
Try it here https://regex101.com/
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')
If I am correct, the following code will only match a URL that is exactly as presented.
However, what would it look like if you wanted to identify subdomains as well as urls that contain various different query strings - in other words, any address that contains this domain:
var url = /test.com/
if (window.location.href.match(url)){
alert("match!");
}
If you want this regex to match "test.com" you need to escape the "." and both of the "/" that means any character in regex syntax.
Escaped : \/test\.com\/
Take a look for here for more info
No, your pattern will actually match on all strings containing test.com.
The regular expresssion /test.com/ says to match for test[ANY CHARACTER]com anywhere in the string
Better to use example.com for example links. So I replaces test with example.
Some example matches could be
http://example.com
http://examplexcom.xyz
http://example!com.xyz
http://example.com?q=123
http://sub.example.com
http://fooexample.com
http://example.com/asdf/123
http://stackoverflow.com/?site=example.com
I think you need to use /g. /g enables "global" matching. When using the replace() method, specify this modifier to replace all matches, rather than only the first one:
var /test.com/g;
If you want to test if an URL is valid this is the one I use. Fairly complex, because it takes care also of numeric domain & a few other peculiarities :
var urlMatcher = /(([\w]+:)?\/\/)?(([\d\w]|%[a-fA-f\d]{2,2})+(:([\d\w]|%[a-fA-f\d]{2,2})+)?#)?([\d\w][-\d\w]{0,253}[\d\w]\.)+[\w]{2,4}(:[\d]+)?(\/([-+_~.\d\w]|%[a-fA-f\d]{2,2})*)*(\?(&?([-+_~.\d\w]|%[a-fA-f\d]{2,2})=?)*)?(#([-+_~.\d\w]|%[a-fA-f\d]{2,2})*)?/;
Takes care of parameters and anchors etc... dont ask me to explain the details pls.
I would like to construct a quoted google search string, and put this string in a link. The user would click on the link and be taken to the quoted search result. (Using JQuery)
$(".abstract_text",this).append('<div> Search For Fulltext</div>');
The above code gives this output:
http://google.com/search?btnI=1&q=The+Q+switched+ND+YAG+laser+effectively+treats+tattoos+in+darkly+pigmented+skin
I would like to find a way to produce this output:
http://google.com/search?btnI=1&q="The+Q+switched+ND+YAG+laser+effectively+treats+tattoos+in+darkly+pigmented+skin"
My question seems to be related to this question but I'm not exactly sure how to apply the answer that was accepted there: Nesting Quotes in JavaScript/HTML
*Please Note that though the search results are the same regardless of the output in this case, they will be different in general.
Solution:Use the encode URI function
$(".abstract_text",this).append('<div> Search For Fulltext</div>');
Output:
http://www.google.com/search?btnI=1&q=%22Early+treatment+of+traumatic+tattoo+by+erbium+YAG+laser%22
I don't know about JQuery but I guess what you want to do is encode your URL's special chars.
You can use the native Javascript function encodeURI() (function reference)
The default encoding for double quotes " is %22.
Try this:
$(".abstract_text", this).append('<div><a href=\'http://google.com/search?btnI=1&q=\"'+art_title.make_search()+'\"\'> Search For Fulltext</a></div>');
I need to use a regex to pull a value out a url domain that will exclude everything but the host (ex: wordpress) and domain type (ex .com). The urls are dynamic and contain 2-3 values for each result (www.example.com or example.org). I am trying to use this expression, but I am only getting back the first letter of every item I am attempting to exclude:
Expresssion
(?!wordpress|com|www)(\w+|\d+)
String
example.wordpress.com
Results
example
ordpress
om
Desired Result
example
Any assistance would be greatly appreciated
Anchor your regular expression:
\b(?!wordpress|com|www)(\w+|\d+)\b
You might also want to consider whether (\w+|\d+) is really what you mean. \w already includes digits. Also, there are other characters allowed in URLs such as -. Do you need to handle this?
If I was to do thing like that, I would take advantage of the format of the url: anything (dot) 2nd-level-domain (dot) 1st-level-domain:
^(?<level3>.*)[.]?(?<level2>.+)[.](?<level1>.+)$
Is it so that you are only after what is after the domain part??
(/\/(?!\/).*?\/(.*)/).exec("http://www.google.com/sdfsdf/fdsff")[1]
// returns sdfsdf/fdsff