javascript regex pattern for _water_glass - javascript

I need a javascript regex pattern to test a schema variable, so that it should have either of the following.
It can start with any character followed by "_water_glass" and must not be anything after water_glass like "xxxx_water_glass"
or
It can be just "water_glass" not necessary to have character before water_glass and must not be anything after water_glass.
Could anyone help on this please to get the regex pattern.

Try this simply /^.*_?\_water_glass/
var re = /^.*_?_water_glass/mg;
var str = 'horse.mp3_country_code\n4343434_country_code\n_country_code';
var m;
while ((m = re.exec(str)) != null) {
if (m.index === re.lastIndex) {
re.lastIndex++;
}
// View your result using the m-variable.
// eg m[0] etc.
}
DEMO https://regex101.com/r/gB9zL7/2

Here you are:
^(?:.+_|)water_glass$
Details:
^- start of string
(?:.+_|) - an optional 1+ chars other than line break chars, as many as possible, up to the last _ including it
water_glass - a water_glass substring
$ - end of string.
See this regex demo and a demo code below:
var re = /^(?:.+_|)water_glass$/gm;
var str = 'xxxx_water_glass\nwater_glass';
var m;
while ((m = re.exec(str)) != null) {
if (m.index === re.lastIndex) {
re.lastIndex++;
}
// View your result using the m-variable.
// eg m[0] etc.
}

Related

How can I create a JavaScript regex for finding email addresses in strings?

I have a logic app which is triggered by emails in an inbox. It is all working, except for some emails are getting through when I don't want them. Or rather an email signature with an image description of image001.png#01D766B1.7C184990 is getting through.
I think it might be my regex that is allowing it, but I am not very good with regex.
Here is the code I have so far:
var reg = /([a-zA-Z0-9._-]+#[a-zA-Z0-9._-]+\.[a-zA-Z0-9_-]+)/gi;
var emailData = " \n\n'Phonenumber2#test.com'\n\n \n\n DevOps\n[cid:image001.png#01D766B1.7C184990]\n\n ";
//Matching email signatures
var matches = emailData .match(reg);
console.log(matches);
I need the regex to return a list of any email addresses, but they need to be fully formed. Unlike the one mentioned above which is missing the .com (or .org etc).
Your regex (allowing everything which has an # and a .)
const regex = /([a-zA-Z0-9._-]+#[a-zA-Z0-9._-]+\.[a-zA-Z0-9_-]+)/gm;
const str = `Phonenumber2#test.com
Phonenumber2#test.info
image001.png#01D766B1.7C184990
Phonenumber2#test.org`;
let m;
while ((m = regex.exec(str)) !== null) {
// This is necessary to avoid infinite loops with zero-width matches
if (m.index === regex.lastIndex) {
regex.lastIndex++;
}
console.log(m[0]);
}
#1 No numbers allowed after last .
const regex = /([a-zA-Z0-9._-]+#[a-zA-Z0-9._-]+\.[a-zA-Z_-]+)/gm;
const str = `Phonenumber2#test.com
Phonenumber2#test.info
image001.png#01D766B1.7C184990
Phonenumber2#test.org`;
let m;
while ((m = regex.exec(str)) !== null) {
// This is necessary to avoid infinite loops with zero-width matches
if (m.index === regex.lastIndex) {
regex.lastIndex++;
}
console.log(m[0]);
}
#2 Restrict characters after last . to be min 2 and max 7 characters {2,7}$
const regex = /([a-zA-Z0-9._-]+#[a-zA-Z0-9._-]+\.[a-zA-Z0-9_-]{2,7}$)/gm;
const str = `Phonenumber2#test.com
Phonenumber2#test.info
image001.png#01D766B1.7C184990
Phonenumber2#test.org`;
let m;
while ((m = regex.exec(str)) !== null) {
// This is necessary to avoid infinite loops with zero-width matches
if (m.index === regex.lastIndex) {
regex.lastIndex++;
}
console.log(m[0]);
}
#3 Define a list of possible top-level domain names like (com|org|info)
const regex = /([a-zA-Z0-9._-]+#[a-zA-Z0-9._-]+\.(com|org|info)$)/gm;
const str = `Phonenumber2#test.com
Phonenumber2#test.info
image001.png#01D766B1.7C184990
Phonenumber2#test.org`;
let m;
while ((m = regex.exec(str)) !== null) {
// This is necessary to avoid infinite loops with zero-width matches
if (m.index === regex.lastIndex) {
regex.lastIndex++;
}
console.log(m[0])
}
have a look at - https://ihateregex.io/expr/email/
Hate to break it to you but email match via Regex are hard if not impossible.
one way would be matching things with domain name TDN endings ( you can create a group of all tdn and match or just limit the end part of regex - modified regex and filter it out from there onwards.

Regex match letters not inside single quote

I need to match all charecters and digits(\w) inside the string which not inside the single quote (\')
For instance I have string:
param : 'test' .param4 'zzzz' param8 * 'rrrr'
from that string I need to get:
- param
- param4
- param8
Thx for any advance.
You can use this lookahead based regex:
/(?=(?:(?:[^']*'){2})*[^']*$)\b\w+\b/gm
RegEx Demo
This regex will match a word if that word is outside single quotes by using a lookahead to make sure there are even number of quotes after each word. This assumes unescaped quotes are balanced.
Code:
var re = /(?=(?:(?:[^']*'){2})*[^']*$)\b\w+\b/gm;
var str = 'param : \'test\' .param4 \'zzzz\' param8 * \'rrrr\' class2.class3*dsaasd';
var m;
var result;
while ((m = re.exec(str)) !== null) {
if (m.index === re.lastIndex)
re.lastIndex++;
res.push(m[0]);
}
console.log(result);

How to use a Regular Expression in JavaScript which contains special characters

Sorry, probably being dumb this morning, but I don't know much about regular expressions, but have created something I want to use with https://regex101.com/
But... I can't use the code they suggest in Javascript without escaping it first.
Here's the regex: (?<=color:\s)([a-z]+)
Which, does what I want (matching a word after color: in a CSS file)
But, the code they suggest to use in JS is:
var re = /(?<=color:\s)([a-z]+)/g;
var str = ' color: black';
var m;
while ((m = re.exec(str)) !== null) {
if (m.index === re.lastIndex) {
re.lastIndex++;
}
// View your result using the m-variable.
// eg m[0] etc.
}
The first line, won't work, so I escaped it to: var re = /\(?<=color:\s\)([a-z]+)/i which stops the Javascript error, but won't match the strings any more.
What am I doing wrong?
As an aside... can anyone point me to expanding this regex to exclude anything followed by a bracket? I am trying to get color names only, so "color: black;" should match, also "box-shadow: black... etc" should match, but ideally, not "color: rgb(... etc"
It is true that JS does not support look-behinds, although there are workarounds:
Reverse the string and then matches that enables using look-aheads
Use capturing groups
Use a while loop with re.lastIndex manipulation
In this case, it is much easier to use the capturing group:
var re = /\bcolor:\s*([a-z]+)/ig;
var str = ' color: black';
var m;
while ((m = re.exec(str)) !== null) {
if (m.index === re.lastIndex) {
re.lastIndex++;
}
// m[1] is holding our value!
document.getElementById("res").innerHTML = m[1];
}
<div id="res"/>

selecting with regex content between two points

I always have a hard time with regex..
I'm trying to select the text between (taking into acount the before and after)
'window.API=' and ';' //for window.API= '--API--';
and other cases like:
'window.img_cdn=' and ';' //for window.img_cdn= '--imgCDN--';
any tips on witch regex concepts I should use would be a great help!
If you want to capture the content between 'xx' you can use a regex like this:
'(.*?)'
working demo
For the sample text:
window.API= '--API--';
window.img_cdn= '--imgCDN--';
You will capture:
MATCH 1
1. [13-20] `--API--`
MATCH 2
1. [40-50] `--imgCDN--`
The javascript code you can use is:
var re = /'(.*?)'/g;
var str = 'window.API= \'--API--\';\nwindow.img_cdn= \'--imgCDN--\';';
var m;
while ((m = re.exec(str)) != null) {
if (m.index === re.lastIndex) {
re.lastIndex++;
}
// View your result using the m-variable.
// eg m[0] etc.
}
On the other hand, if you specifically want to capture the content for only those entries, then you can use this regex:
window\.(?:API|img_cdn).*?'(.*?)'
If you want to match any text between a <some string>= sign and a semicolon, here you go:
(?:[\w\.]+\s*=\s')(.+)(?:';)$
This regex pattern will match a full string if an escaped apostrophe is present in the string: //for window.img_cdn = '--imgCDN and \'semicolon\'--';
JavaScript code:
var re = /(?:[\w\.]+\s*=\s')(.+)(?:';)$/gm;
var str = '//for window.img_cdn= \'--imgCDN--\';\n//for window.img_cdn = \'--imgCDN and semicolon = ;;;--\';';
var m;
while ((m = re.exec(str)) != null) {
if (m.index === re.lastIndex) {
re.lastIndex++;
}
// view results
}
The required text is in the 1st captured group. In case there is a semicolon in the text you are looking for, you will correctly match it due to the $ anchor.
See demo here

Regex trouble in JavaScript

I am trying to extract data from a string using RegEx , but i am getting a NULL value as result.
here is my current code
var re = /(\[cid=(?:[0-9]*)(?:(?:,\[[^]]*\][^]]*)?|(?:,[^]]*))\])/;
var str = '[cid=5555,[CONSTIMG]5555.jpg]The Sample text is awesome';
var m;
while ((m = re.exec(str)) != null) {
if (m.index === re.lastIndex) {
re.lastIndex++;
}
}
console.log(m[0]);
The value that i am getting back is NULL.
If anyone can point me in the right direction, that would be very helpful.
Thanks in advance.
Your expression matches PCRE regular expression syntax but not JavaScript because JavaScript requires that square brackets inside a character class be escaped with \. This is what you want:
(\[cid=(?:\d*)(?:(?:,\[[^\]]*\][^\]]*)?|(?:,[^\]]*))\])
Explained: https://regex101.com/r/pN4vP4/2

Categories