Matching cookie string in Javascript with regex - javascript

For simplicity sake, I am looking to match a cookie in JavaScript with two groups. This is so I can loop through the results in a key, pair order.
I am testing with this string:
_ga_GZ83393=NOTGOOGLE.1.1613155331397.4.1.12345678.4; _ga=GA5.2.14144141.1135335686424; test=bob
So far I have come up with /(\w+)=(\w+)/ but it is failing with the periods that are in the Google analytic cookies.
NOTE: The Google Analytic values are spoofed, keeping the same format but as to not cause security issues.

You can scan up to the next ; or end of string:
/(\w+)=([^;]*)/
You could use split and a regex to pick the key & value pairs:
const cookies = '_ga_GZ83393=NOTGOOGLE.1.1613155331397.4.1.12345678.4; _ga=GA5.2.14144141.1135335686424; test=bob';
const regex = /^ *([^=]*)=(.*)$/;
cookies.split(/;/).forEach(item => {
let key = item.replace(regex, '$1');
let val = item.replace(regex, '$2');
console.log('key: ' + key + ', val: ' + val);
});
Output:
key: _ga_GZ83393, val: NOTGOOGLE.1.1613155331397.4.1.12345678.4
key: _ga, val: GA5.2.14144141.1135335686424
key: test, val: bob

/((?<index>\w+)=(?<value>[\w\.0-9]+))/g
Thanks to #Peter Thoeny to point out that the named groups are not supported in all browsers, so here is the version without it.
/((\w+)=([\w\.0-9]+))/g
https://regex101.com/r/lWpbgz/1
var cookie = "_ga_GZ83393=NOTGOOGLE.1.1613155331397.4.1.12345678.4; _ga=GA5.2.14144141.1135335686424; test=bob";
var match = null;
var regex = /((?<index>\w+)=(?<value>[\w\.0-9]+))/g;
var div = document.querySelector("#result");
match = regex.exec(cookie);
while(match != null) {
div.innerText += match.groups.index + " = " + match.groups.value + "\n";
console.log(match.groups.index + " = " + match.groups.value);
match = regex.exec(cookie);
}
<div id="result"></div>

Related

How to check for Forward Slash within this Regex for all special characters?

I am trying to find a regex solution to check if a string matches all conditions + / forward slashes.
Current code:
var specialChars = /^[a-zA-Z0-9!##\$%\^\&*\)\(+=._-]+$/g;
This will match true if a string looks like so: 4!##$.
However it does not work if the string looks like this: 5/6/2019
This is how I'm implementing this check, basically I have a function that takes in an long string. And what I'm trying to do is pluck out the tracking ID then create a link out of it.
My test cases are also in the demo, the date test is the one that fails, since the linkCreator function ends up linking to the date:
https://jsfiddle.net/cojuevp5/
var linkCreator = function(value) {
var strings = value.split(' ');
var aHref = '<a href="http://www.google.com/search?q=';
var targetBlank = '" target="_blank" style="text-decoration: underline">';
var trackingString = strings.reduce(function(prevVal, currVal, idx) {
var specialChars = /^[a-zA-Z0-9!##\$%\^\&*\)\(+=._-]+$/g;
// Does val start with number and not contain special characters including /
var link = currVal.match(/^\d/) && !currVal.match(specialChars) ?
aHref + currVal + targetBlank + currVal + '</a>' :
currVal;
return idx == 0 ? link : prevVal + ' ' + link;
}, '');
console.log(trackingString);
}
const case1 = '434663008870'
const case2 = '4S4663008870'
const case3 = '4S4663008870 PS'
const case4 = 'SHD FX 462367757727 PS'
const case5 = 'SHD FX 429970755485, R'
const case6 = 'SHD HEADER TRACKING PS'
const case7 = 'N/A'
const case8 = 'AF SHD FX 462367757727 PS'
const case9 = '4/7/2019'
const case10 = '4!##$%^&'
const value = case9
const link = linkCreator(value)
console.log(link)
You might want to add a \/ and that would likely solve your problem:
^([A-z0-9!\/##$%^&*)(+=._-]+)$
Just like Barmar says, you do not need to escape all chars inside []:
I'm guessing that this may be what you might want to match:
You might just use this tool and design any expression that you wish.
Graph
This graph shows how your expression works:

Convert String in the Parenthesis in the URL To Query Parameters: Javascript

I want to convert the string in {} in search URL to query parameters which would help users capture search terms in web analytics tools.
Here's what I am trying to do, Let's say
Search URL is:
example.com/search/newyork-gyms?dev=desktop&id=1220391131
User Input will be:
var search_url_format = '/search/{city}-{service}
Output URL:
example.com/search?city=newyork&service=gyms&dev=desktop&id=1220391131
The problem is when is use the regex {(.*)} it captures the whole string {city}-{service}.
But I what I want is [{city},{service}].
The URL format can also be like
search/{city}/{service}/
search/{city}_{service}/
What I have tried is for a single variable.
It returns correct output.
Eg: URL:/search/newyork
User Input: /search/{city}
Output: /search/newyork?city=newyork
URL: /search-germany
User Input: /search-{country}
Output: /search-germany?country=germany
var search_url_format = '/search/{city}' //User Enters any variable in brackets
var urloutput = '/search/newyork' //Demo URL
//Log output
console.log(URL2Query(search_url_format)) //Output: '/search/newyork?city=newyork'
function URL2Query(url) {
var variableReg = new RegExp(/{(.*)}/)
var string_url = variableReg.exec(url)
var variable1 = string_url[0]
//Capture the variable
var reg = new RegExp(url.replace(variable1, '([^\?|\/|&]+)'))
var search_string = reg.exec(urloutput)[1]
if (location.search.length > 0) // if no query parameters
{
return urloutput + "?" + string_url[1] + "=" + search_string
} else {
return urloutput + "&" + string_url[1] + "=" + search_string
}
}
You are missing two things:
parenthesis to match groups and you use .* which includes "{" sign.
So can use match instead of exec like this:
var search_url_format = '/search/{city}-{service}' //User Enters any
var variableReg = new RegExp(/({\w+})/g)
var string_url = url.match(variableReg); // [{city}, {service}]
You can probably assume your "variable" will be alphanumeric instead of any character. With this assumption "{", "-", "_" etc will be punctuation.
so your grouping regexp could be /({\w+})/g.
//example
const r = /({\w+})/g;
let variable;
const url = '/search/{city}-{service}';
while ((variable = r.exec(url)) !== null) {
let msg = 'Found ' + variable[0] + '. ';
msg += 'Next match starts at ' + r.lastIndex;
console.log(msg);
}

How do you apply regex to value in database to query against in mongodb?

Here is the scenario, you are storing phone numbers in mongo as a string. Values could be any of the following:
987-654-3210
9876543210
(978) 654-3210
All of these numbers are the same. I want to search using a RegExp which will basically strip the value of all non alphanumeric characters. Searching using 987-654-3210 will return 9876543210, but that's it. I'm hoping to find a solution where the regex will be applied to what is stored in the database to match the regexp that I'm passing in the query.
Ultimately, if all three of those phone numbers are in the database and the user searches using any of the three, all three should be returned.
You can use those 3 regex that will extract the 3 expected groups (in your example 987, 654 and 3210) :
XXX-XXX-XXXX : /(\d{3})-(\d{3})-(\d{4})/g
XXXXXXXXX : /(\d{3})(\d{3})(\d{4})/g
(XXX) XXX-XXXX : /\((\d{3})\)\s(\d{3})-(\d{4})/g
The idea is to generate those 3 groups from your input and generate the three format from these groups to find all documents matching any of those three formats :
var input = "987-654-3210"; // or any of the 3 formats
var regex = [];
regex[0] = /(\d{3})-(\d{3})-(\d{4})/g; // 987-654-3210
regex[1] = /(\d{3})(\d{3})(\d{4})/g; // 9876543210
regex[2] = /\((\d{3})\)\s(\d{3})-(\d{4})/g; // (978) 654-3210
function getPhoneNumber(phoneNumber) {
for (key in regex) {
var match = regex[key].exec(phoneNumber);
if (match) {
return match;
}
}
}
var group = getPhoneNumber(input);
var filter = [];
filter[0] = group[1] + "-" + group[2] + "-" + group[3];
filter[1] = group[1] + group[2] + group[3];
filter[2] = "(" + group[1] + ") " + group[2] + "-" + group[3];
db.data.find({
"phoneNumber": {
"$in": filter
}
})
You can try it directly in the mongodb shell

javascript regex ending string with values in array

I've to check if my string value is ending with ".com" or ".de"
I've put this values inside array:
var valuesEnd = [".com", ".de"]
My value to compare is taken from form. Also it has to check for # sign, it must have.
Use RegExp#test.
var str = ['abc.de', 'wok.pl', 'qwdok.com'];
console.log(str.map(v => /\w+\.(de|com)$/g.test(v) ? v + ' is valid' : v + ' is invalid'));
You can use a regular expression like this:
var str = prompt("Email: ");
if(/\.(?:com|de)$/.test(str))
alert("'" + str + "' is valid");
else
alert("'" + str + "' is not valid");
I've created a jQuery plugin for you:
(function($) {
// the string object (NOTE: it can be extended)
$.string = {};
// "create" a new string
$.string.new = function(string) {
return string;
};
// test a string for a regex match
$.string.test = function(string, regex) {
return regex.test(string);
};
// join three strings together
$.string.join = function(a, b, c) {
return a + b + c;
};
// log a string to the console
$.string.log = function(string) {
console.log(string);
};
})(jQuery);
Then you would use the plugin like this:
// for test purposes we'll be using: "example#example.com"
var email = $.string.new("example#example.com");
// check to see if the email is valid or not
if($.string.test(email, /\#.*?\.(?:com|de)$/)) {
// let us know that the email is valid
$.string.log($.string.join("'", email, "' is a valid email."));
}
// if this block runs the email is invalid
else {
// let us know that this is an invalid email
$.string.log($.string.join("'", email, "' is not a valid email."));
}

A simpler way to capture multiple variables in javascript regexps

Comming from the perl/python world I was wondering if there is a simpler way to filter out multiple captured variables from regexp in javascript:
#!/usr/bin/env node
var data=[
"DATE: Feb 26,2015",
"hello this should not match"
];
for(var i=0; i<data.length; i++) {
var re = new RegExp('^DATE:\\s(.*),(.*)$');
if(data[i].match(re)) {
//match correctly, but how to get hold of the $1 and $2 ?
}
if(re.exec(data[i])) {
//match correctly, how to get hold of the $1 and $2 ?
}
var ret = '';
if(data[i].match(re) && (ret = data[i].replace(re,'$1|$2'))) {
console.log("line matched:" + data[i]);
console.log("return string:" + ret);
ret = ret.split(/\|/g);
if (typeof ret !== 'undefined') {
console.log("date:" + ret[0], "\nyear:" + ret[1]);
}
else {
console.log("match but unable to parse capturing parentheses");
}
}
}
The last condition works, but you need a temp var and split it, and you need to have a test in front because the replace works on everything.
Output is:
$ ./reg1.js
line matched:DATE: Feb 26,2015
return string:Feb 26|2015
date:Feb 26
year:2015
If I look up: mosdev regexp it says on (x):
The matched substring can be recalled from the resulting array's
elements 1, ..., [n] or from the predefined RegExp object's
properties $1, ..., $9.
How do I get hold of the RegExp objects' $1 and $2?
Thanks
The MDN is a good resource for learning Javascript. In this particular case, .match(), .exec(), etc. all return objects containing match information. That is where you'll find captured groups.
Thanks for the answer found that they return an array:, so the simpler blocks can look like this:
if((ret = data[i].match(re))!=null) {
//match correctly, but how to get hold of the $1 and $2 ?
console.log("line matched:" + data[i]);
console.log("return string:" + ret[0] + "|" + ret[1]);
ret = null;
}
if((ret = re.exec(data[i]))!=null) {
//match correctly, how to get hold of the $1 and $2 ?
console.log("line matched:" + data[i]);
console.log("return string:" + ret[0] + "|" + ret[1]);
ret = null;
}
Using JavaScript .test() and .match() this can be very simple
An example:
var input = "DATE: Feb 26, 2015",
regex = /^DATE:\s*(.*),\s*(.*)$/;
if (regex.match(input)) {
console.log('Matches Format!');
//.match() needs splicing because .match() returns the actually selected stuff. It becomes weirder with //g
var results = input.match(regex).splice(0,1);
console.log(results);
//Logs: ["Feb 26", "2015"]
}
Regex101 can be useful

Categories

Resources