Selecting entire string using Regex - javascript

I need help with a regex to select this entire string
%2526_ga%253d1.193373933.1506621463.1391436765&
this exact match is always there:
%2526_ga%253d
there's always a random number like this, but don't focus on the period since there's other periods later in the URL similar to this:
1.193373933.1506621463.1391436765&
This is what I have so far:
var str = window.location.search.match('_ga%253d');
alert(str);

If you want your prefix plus any digits full stops until the first etcetera the regex below should do
/_ga%253d'([0-9\.])+&/

Related

Generate regex to match with the url

I have a string want those product hubs vendor matched properly with their respective position and digit between them must digit but.digits can vary in numbers.
pattern: /products/8236/hubs/1/vendor
url="https://d3skctyxg9sfpd.cloudfront.net/sadassdsad/products/8236/hubs/1/vendor"
this.tests = "products/8236/hubs/1/vendor";
var fields = this.tests.split('_qa_current');
console.log(fields, "fielding values fetched");
/products/.test(fields);
console.log(/products/.test(this.tests), "url getting");
/^products\[0-9]\\hubs\[0-9]\\vendors$/.test(fields[1]);
This should be your regex
^\/products\/\d+\/hubs\/\d+\/vendor$
The slashes are not escaped correctly in your regex.
Code
this.tests = "https://d3skctyxg9sfpd.cloudfront.net/milkbasket_qa_current/products/8236/hubs/1/vendor";
var fields = this.tests.split('_qa_current');
console.log(fields, "fielding values fetched");
/products/.test(fields);
console.log(/products/.test(this.tests), "url getting");
/^\/products\/\d+\/hubs\/\d+\/vendor$/.test(fields[1]);
Regex: /-\/products\/\d+\/hubs\/\d+\/vendor/g
You can replace the digit with the \d+ meaning there can be one or more numbers in this place.
Here's the website to try the pattern and see for yourself: https://regexr.com/.
There are also explanations on the bottom of the screen to help you understand better what the rules do.

Match a string between two other strings with regex in javascript

How can I use regex in javascript to match the phone number and only the phone number in the sample string below? The way I have it written below matches "PHONE=9878906756", I need it to only match "9878906756". I think this should be relatively simple, but I've tried putting negating like characters around "PHONE=" with no luck. I can get the phone number in its own group, but that doesn't help when assigning to the javascript var, which only cares what matches.
REGEX:
/PHONE=([^,]*)/g
DATA:
3={STATE=, SSN=, STREET2=, STREET1=, PHONE=9878906756,
MIDDLENAME=, FIRSTNAME=Dexter, POSTALCODE=, DATEOFBIRTH=19650802,
GENDER=0, CITY=, LASTNAME=Morgan
The way you're doing it is right, you just have to get the value of the capture group rather than the value of the whole match:
var result = str.match(/PHONE=([^,]*)/); // Or result = /PHONE=([^,]*)/.exec(str);
if (result) {
console.log(result[1]); // "9878906756"
}
In the array you get back from match, the first entry is the whole match, and then there are additional entries for each capture group.
You also don't need the g flag.
Just use dataAfterRegex.substring(6) to take out the first 6 characters (i.e.: the PHONE= part).
Try
var str = "3={STATE=, SSN=, STREET2=, STREET1=, PHONE=9878906756, MIDDLENAME=, FIRSTNAME=Dexter, POSTALCODE=, DATEOFBIRTH=19650802, GENDER=0, CITY=, LASTNAME=Morgan";
var ph = str.match(/PHONE\=\d+/)[0].slice(-10);
console.log(ph);

Extract number between slashes

This is my setup:
var test =
"http://tv.website.com/video/8086844/randomstring/".match(/^.+tv.website.com\/video\/(.*\d)/);
I want to extract the video id(8086844) and the regex does work, however when another digit is added after the "randomstring", it also matches the 8086844 + randomstring + other number.
What do I have to change in order to always get just the video id.
Try this regex
/^.+tv.website.com\/video\/([\d]+)/
It will search every digit character after ...video\ word and then give all the concordant digits thereafter till any non-digit character comes
The problem is the (.*\d) part, it looks for a greedy string ends with a digit, instead you need a continues series of digits after video/, it can be done via (\d+)
change it to
var test = "http://tv.website.com/video/8086844/randomstring/dd".match(/^.+tv.website.com\/video\/(\d+)/)[1];
var test = "http://tv.website.com/video/8086844/randomstring/8";
test = test.match(/^.+tv.website.com\/video\/(\d+)/);
console.log(test);
console.log(test[1]);
Output
[ 'http://tv.website.com/video/8086844',
'8086844',
index: 0,
input: 'http://tv.website.com/video/8086844/randomstring/8' ]
8086844
You are almost there. We know that, its going to be only numbers. So instead of .*\d we are gathering only the digits and grouping them using parens.
This is the simplest of all:
Use substr here
test.substr(28,7)
Fiddle
To extract out the id from your string test:We use substr(from,to)

Regex to capture everything but consecutive newlines

What is the best way to capture everything except when faced with two or more new lines?
ex:
name1
address1
zipcode
name2
address2
zipcode
name3
address3
zipcode
One regex I considered was /[^\n\n]*\s*/g. But this stops when it is faced with a single \n character.
Another way I considered was /((?:.*(?=\n\n)))\s*/g. But this seems to only capture the last line ignoring the previous lines.
What is the best way to handle similar situation?
UPDATE
You can consider replacing the variable length separator with some known fixed length string not appearing in your processed text and then split. For instance:
> var s = "Hi\n\n\nBye\nCiao";
> var x = s.replace(/\n{2,}/, "#");
> x.split("#");
["Hi", "Bye
Ciao"]
I think it is an elegant solution. You could also use the following somewhat contrived regex
> s.match(/((?!\n{2,})[\s\S])+/g);
["Hi", "
Bye
Ciao"]
and then process the resulting array by applying the trim() string method to its members in order to get rid of any \n at the beginning/end of every string in the array.
((.+)\n?)*(you probably want to make the groups non-capturing, left it as is for readability)
The inner part (.+)\n? means "non-empty line" (at least one non-newline character as . does not match newlines unless the appropriate flag is set, followed by an optional newline)
Then, that is repeated an arbitrary number of times (matching an entire block of non-blank lines).
However, depending on what you are doing, regexp probably is not the answer you are looking for. Are you sure just splitting the string by \n\n won't do what you want?
Do you have to use regex? The solution is simple without it.
var data = 'name1...';
var matches = data.split('\n\n');
To access an individual sub section split it by \n again.
//the first section's name
var name = matches[0].split('\n')[0];

javascript regex to extract the first character after the last specified character

I am trying to extract the first character after the last underscore in a string with an unknown number of '_' in the string but in my case there will always be one, because I added it in another step of the process.
What I tried is this. I also tried the regex by itself to extract from the name, but my result was empty.
var s = "XXXX-XXXX_XX_DigitalF.pdf"
var string = match(/[^_]*$/)[1]
string.charAt(0)
So the final desired result is 'D'. If the RegEx can only get me what is behind the last '_' that is fine because I know I can use the charAt like currently shown. However, if the regex can do the whole thing, even better.
If you know there will always be at least one underscore you can do this:
var s = "XXXX-XXXX_XX_DigitalF.pdf"
var firstCharAfterUnderscore = s.charAt(s.lastIndexOf("_") + 1);
// OR, with regex
var firstCharAfterUnderscore = s.match(/_([^_])[^_]*$/)[1]
With the regex, you can extract just the one letter by using parentheses to capture that part of the match. But I think the .lastIndexOf() version is easier to read.
Either way if there's a possibility of no underscores in the input you'd need to add some additional logic.

Categories

Resources