Javascript Storing Regexp screws the original format - regex

I have an angular app where a user can add a regexp in a form, a value like:
github\.com/([A-Za-z0-9\-\_]+)/([A-Za-z0-9\-\_]+)
When I store this in the localStorage and I inspect the localStorage:
github\\\\.com\\/([A-Za-z0-9\\\\-\\\\_]+)\\/([A-Za-z0-9\\\\-\\\\_]+)
When I retrieve in Javascript elsewhere this value I get:
github\\.com\/([A-Za-z0-9\\-\\_]+)\/([A-Za-z0-9\\-\\_]+)
This is not the original regexp and the match method in Javascript can't work.
NOTE: after submitting the form, I store the object with:
localStorage.myobject = JSON.stringify(myobject);

You can get rid of overescaping here, just use
github[.]com/([A-Za-z0-9_-]+)/([A-Za-z0-9_-]+)
and initialize it via a RegExp constructor so as not to have to escape the regex delimiter /. A dot inside [] loses its special meaning and only matches a literal dot, the hyphen at the end of the character class only matches a literal hyphen, and the _ does not have to be escaped at all anywhere in the pattern:
var tst = "github.com/Test1-Text/Test2";
var pattern = "github[.]com/([A-Za-z0-9_-]+)/([A-Za-z0-9_-]+)";
console.log(new RegExp(pattern).test(tst));
UPDATE:
When using patterns from external sources, you need to use the constructor notation. Make sure your regex patterns are stored as literal strings (if you had RegExp("C:\\\\Folder"), make sure it is stored as C:\\Folder), and when reading the value in it will be automatically be usable with the RegExp constructor.

Related

What is the regular expression for all pages except "/"?

I am using NextAuth for Next.js for session management. In addition, I am using the middleware.js to protect my routes from unauthenticated users.
According to https://nextjs.org/docs/advanced-features/middleware#matcher,
if we want to exclude a path, we do something like
export const config = {
matcher: [
/*
* Match all request paths except for the ones starting with:
* - api (API routes)
* - static (static files)
* - favicon.ico (favicon file)
*/
'/((?!api|static|favicon.ico).*)',
],
}
In this example, we exclude /api, /static,/favicon.icon. However, I want to exclude all path except the home page, "/". What is the regular expression for that? I am tried '/(*)'. It doesn't seem to work.
The regular expression which matches everything but a specific one-character string / is constructed as follows:
we need to match the empty string: empty regex.
we need to match all strings two characters long or longer: ..+
we need to match one-character strings which are not that character: [^/].
Combining these three together with the | branching operator: "|..+|[^/]".
If we are using a regular expression tool that performs substring searching rather than a full match, we need to use its anchoring features; perhaps it supports the ^ and $ notation for that: "^(|..+|[^/])$".
I'm guessing that you might not want to match empty strings; in which case, revise your requirement and drop that branch from the expression.
Suppose we wanted to match all strings, except for a specific fixed word like abc. Without negation support in the regex language, we can use a generalization of the above trick.
Match the empty string, like before, if desired.
Match all one-character strings: .
Match all two-character strings: ..
Match all strings longer than three characters: ....+
Those simple cases taken care of, we focus on matching just those three-symbol strings that are not abc. How can we do that?
Match all three-character strings that don't start with a: [^a]...
Match all three-character strings that don't have a b in the middle: .[^b].
Match all three-character strings that don't end in c: ..[^c].
Combine it all together: "|.|..|....+|[^a]..|.[^b].|..[^c]".
For longer words, we might want to take advantage of the {m,n} notation, if available, to express "match from zero to nine characters" and "match eleven or more characters".
I will need to exclude the signin page and register page as well. Because, it will cause an infinite loop and an error, if you don't exclude signin page. For register page, you won't be able to register if you are redirected to the signin page.
So the "/", "/auth/signin", and "/auth/register" will be excluded. Here is what I needed:
export const config = {
matcher: [
'/((?!auth).*)(.+)'
]
}

Matching everything in a cookie string except the value after a specific key

Cookie string:
ViewerUserId=134; ThisUserId=124; session=ntrhtrh5ttrg54htrth54654654trtrgtgtrgtr
I'm havingVarnish which uses PCRE to cache content and I'd like to use regular expression to match everything except the value after ThisUserId, in this case, it should be 124.
I am able to extract 124 by using ThisUserId=([0-9]+)\b or (?:^|;\s*)ThisUserId=(.*?)(?:;|,(?!\s)), however, to set a variable in Varnish, I need to match everything except the target value and then replace the matched strings with empty string, sort of like this:
set req.http.ThisUserId = regsub(req.http.Cookie,"MATCH_EVEYRTHING_EXCEPT_THISUSERID","");
set req.http.ViewerUserId = regsub(req.http.Cookie,"MATCH_EVEYRTHING_EXCEPT_VIEWERUSERID","");
if(req.http.ViewerUserId == req.http.ThisUserId){
return(pass); // Don't cache if viewer is browsing his own pages
}
Can anyone show me an example for matching anything but an unknown value after a specific string?
You can do this with a backreference. This means you select some portion of the regexp with parentheses and then can reference them in the substitution string. So, for example:
set req.http.ThisUserId = regsub(req.http.Cookie,".*ThisUserId=(\d+).*","\1");
set req.http.ViewerUserId = regsub(req.http.Cookie,".*ViewUserId=(\d+).*","\1");

dart regex matching and get some information from it

For practice, I decided to build something like a Backbone router. The user only needs to give the regex string like r'^first/second/third/$' and then hook that to a View.
For Example, suppose I have a RegExp like this :
String regexString = r'/api/\w+/\d+/';
RegExp regExp = new RegExp(regexString);
View view = new View(); // a view class i made and suppose that this view is hooked to that url
And a HttRequest point to /api/topic/1/ and that would match that regex, then i can rendered anything hook to that url.
The problem is, from the regex above, how do i know that \w+ and \d+ value is topic and 1.
Care to give me some pointers anyone? Thank you.
You need to put the parts you want to extract into groups so you can extract them from the match. This is achieved by putting a part of the pattern inside parentheses.
// added parentheses around \w+ and \d+ to get separate groups
String regexString = r'/api/(\w+)/(\d+)/'; // not r'/api/\w+/\d+/' !!!
RegExp regExp = new RegExp(regexString);
var matches = regExp.allMatches("/api/topic/3/");
print("${matches.length}"); // => 1 - 1 instance of pattern found in string
var match = matches.elementAt(0); // => extract the first (and only) match
print("${match.group(0)}"); // => /api/topic/3/ - the whole match
print("${match.group(1)}"); // => topic - first matched group
print("${match.group(2)}"); // => 3 - second matched group
however, the given regex would also match "/api/topic/3/ /api/topic/4/" as it is not anchored, and it would have 2 matches (matches.length would be 2) - one for each path, so you might want to use this instead:
String regexString = r'^/api/(\w+)/(\d+)/$';
This ensures that the regex is anchored exactly from beginning to the end of the string, and not just anywhere inside the string.

Using RegExp for separate each five-character by '-' or something

Hello i have a TextField on my scene. It haves only digits, user input them by clicking on buttons (NumPad). I want to separate each five-character by '-' . User must input 20 digits
I'm using this code
var rexp:RegExp = new RegExp("/^((\d{0,5})(\d{0,5})(\d{0,5})(\d{0,5}))$/");
trace(rexp.test(textEnter.text)); //always false
var s:String=textEnter.text.replace(rexp, "$2-$3-$4-$5");
trace(s);//format is fails
textEnter.text = s;
On this site my patterns are working fine. But they aren't working in my AS3 script.
can you help me and say what i'm doing wrong.
There are two ways to instantiate a regex in AS3. You can either use the constructor form or the regex literal form.
Literal form
With this notation, the regex literal creates an implicit regex object, so you don't have to. The following works fine:
var rexp:RegExp = /^((\d{0,5})(\d{0,5})(\d{0,5})(\d{0,5}))$/;
Constructor form
If you really want to use the constructor form to instantiate your regex, then you replace the delimiting forward slashes with quotation marks. You must also add a second backslash to any metasequences.
var rexp:RegExp = new RegExp("^((\\d{0,5})(\\d{0,5})(\\d{0,5})(\\d{0,5}))$");
More info: http://help.adobe.com/en_US/as3/dev/WS5b3ccc516d4fbf351e63e3d118a9b90204-7e92.html

RegEx check if string contains certain value

I need some help with writing a regex validation to check for a specific value
here is what I have but it don't work
Regex exists = new Regex(#"MyWebPage.aspx");
Match m = exists.Match(pageUrl);
if(m)
{
//perform some action
}
So I basically want to know when variable pageUrl will contains value MyWebPage.aspx
also if possible to combine this check to cover several cases for instance MyWebPage.aspx, MyWebPage2.aspx, MyWebPage3.aspx
Thanks!
try this
"MyWebPage\d*\.aspx$"
This will allow for any pages called MyWebPage#.aspx where # is 1 or more numbers.
if (Regex.Match(url, "MyWebPage[^/]*?\\.aspx")) ....
This will match any form of MyWebPageXXX.aspx (where XXX is zero or more characters). It will not match MyWebPage/test.aspx however
That RegEx should work in the case that MyWebPage.aspx is in your pageUrl, albeit by accident. You really need to replace the dot (.) with \. to escape it.
Regex exists = new Regex(#"MyWebPage\.aspx");
If you want to optionally match a single number after the MyWebPage bit, then look for the (optional) presence of \d:
Regex exists = new Regex(#"MyWebPage\d?\.aspx");
I won't post a regex, as others have good ones going, but one thing that may be an issue is character case. Regexs are, by default, case-sensitive. The Regex class does have a static overload of the Match function (as well as of Matches and IsMatch) which takes a RegexOptions parameter allowing you to specify if you want to ignore case.
For example, I don't know how you are getting your pageUrl variable but depending on how the user typed the URL in their browser, you may get different casings, which could cause your Regex to not find a match.