Regex that only matches when 4 specific words were found - javascript

As the title says, I need a Regex to check if a string has this four words (Update, Rollback, Skip, Not Now) and only return them if all of them are present, if not it doesn’t return anything.
Here is an example:
{"Update":"iVBORw0KGgo","Rollback":"iVBORw0KGgo","Skip":"iVBORw0KGgo","Not Now":"iVBORw0KGgo"}
In this case, it should return [Update, Rollback, Skip, Not Now]
{"Update":"iVBORw0KGgo","Skip":"iVBORw0KGgo","Not Now":"iVBORw0KGgo"}
In this case, it shouldn’t return any value
I tried to create one by myself but my knowledge of Regex is very basic:
(Update|Rollback|Skip|Not Now)
Thanks in advance!
EDIT
I noticed that Regex might not be the best way to achieve this.

Use this:
^(?=.*"Update"\s*:)(?=.*"Rollback"\s*:)(?=.*"Skip"\s*:)(?=.*"Not Now"\s*:)
(?=...) means that if you lookahead, you find this pattern.
So an empty match will return as soon as we find all these 4 patterns.
demo

As an alternative to regex, you can use JSON.parse to parse the string into an object and Object.keys to get the properties:
const str = `{"Update":"iVBORw0KGgo","Rollback":"iVBORw0KGgo","Skip":"iVBORw0KGgo","Not Now":"iVBORw0KGgo"}`;
const keys = Object.keys(JSON.parse(str))
const result = keys.sort().toString() == "Not Now,Rollback,Skip,Update" ? keys : "";
console.log(result)

While regex is clearly not a good tool for the job,
you can do something like this:
re.match("(?=.*Update.*)(?=.*Skip.*)",string)
"(?=WORD)" matches if the expression follows, but doesn't consume any of the string.
Of course, complete the regex by all 4 words that you want similarly.
Also notice that regex by itself doesn't return anything. You need to code for this.

Related

regex to remove certain characters at the beginning and end of a string

Let's say I have a string like this:
...hello world.bye
But I want to remove the first three dots and replace .bye with !
So the output should be
hello world!
it should only match if both conditions apply (... at the beginning and .bye at the end)
And I'm trying to use js replace method. Could you please help? Thanks
First match the dots, capture and lazy-repeat any character until you get to .bye, and match the .bye. Then, you can replace with the first captured group, plus an exclamation mark:
const str = '...hello world.bye';
console.log(str.replace(/\.\.\.(.*)\.bye/, '$1!'));
The lazy-repeat is there to ensure you don't match too much, for example:
const str = `...hello world.bye
...Hello again! Goodbye.`;
console.log(str.replace(/\.\.\.(.*)\.bye/g, '$1!'));
You don't actually need a regex to do this. Although it's a bit inelegant, the following should work fine (obviously the function can be called whatever makes sense in the context of your application):
function manipulate(string) {
if (string.slice(0, 3) == "..." && string.slice(-4) == ".bye") {
return string.slice(4, -4) + "!";
}
return string;
}
(Apologies if I made any stupid errors with indexing there, but the basic idea should be obvious.)
This, to me at least, has the advantage of being easier to reason about than a regex. Of course if you need to deal with more complicated cases you may reach the point where a regex is best - but I personally wouldn't bother for a simple use-case like the one mentioned in the OP.
Your regex would be
const rx = /\.\.\.([\s\S]*?)\.bye/g
const out = '\n\nfoobar...hello world.bye\nfoobar...ok.bye\n...line\nbreak.bye\n'.replace(rx, `$1!`)
console.log(out)
In English, find three dots, anything eager in group, and ending with .bye.
The replacement uses the first match $1 and concats ! using a string template.
An arguably simpler solution:
const str = '...hello world.bye'
const newStr = /...(.+)\.bye/.exec(str)
const formatted = newStr ? newStr[1] + '!' : str
console.log(formatted)
If the string doesn't match the regex it will just return the string.

Use only one of the characters in regular expression javascript

I guess that should be smth very easy, but I'm stuck with that for at least 2 hours and I think it's better to ask the question here.
So, I've got a reg expression /&t=(\d*)$/g and it works fine while it is not ?t instead of &t in url. I've tried different combinations like /\?|&t=(\d*)$/g ; /\?t=(\d*)$|/&t=(\d*)$/g ; /(&|\?)t=(\d*)$/g and various others. But haven't got the expected result which is /\?t=(\d*)$/g or /&t=(\d*)$/g url part (whatever is placed to input).
Thx for response. I think need to put some details here. I'm actually working on this peace of code
var formValue = $.trim($("#v").val());
var formValueTime = /&t=(\d*)$/g.exec(formValue);
if (formValueTime && formValueTime.length > 1) {
formValueTime = parseInt(formValueTime[1], 10);
formValue = formValue.replace(/&t=\d*$/g, "");
}
and I want to get the t value whether reference passed with &t or ?t in references like youtu.be/hTWKbfoikeg?t=82 or similar one youtu.be/hTWKbfoikeg&t=82
To replace, you may use
var formValue = "some?some=more&t=1234"; // $.trim($("#v").val());
var formValueTime;
formValue = formValue.replace(/[&?]t=(\d*)$/g, function($0,$1) {
formValueTime = parseInt($1,10);
return '';
});
console.log(formValueTime, formValue);
To grab the value, you may use
/[?&]t=(\d*)$/g.exec(formValue);
Pattern details
[?&] - a character class matching ? or &
t= - t= substring
(\d*) - Group 1 matching zero or more digits
$ - end of string
/\?t=(\d*)|\&t=(\d*)$/g
you inverted the escape character for the second RegEx.
http://regexr.com/3gcnu
I want to thank you all guys for trying to help. Special thanks to #Wiktor Stribiżew who gave the closest answer.
Now the piece of code I needed looks exactly like this:
/[?&]t=(\d*)$/g.exec(formValue);
So that's the [?&] part that solved the problem.
I use array later, so /\?t=(\d*)|\&t=(\d*)$/g doesn't help because I get an array like [t&=50,,50] when reference is & type and the correct answer [t?=50,50] when reference is ? type just because of the order of statements in RegExp.
Now, if you're looking for a piece of RegExp that picks either character in one place while the rest of RegExp remains the same you may use smth like this [?&] for the example where wanted characters are ? and &.

pretty complex Regex

I did a lot of research, but apparently my Regex skills are not enough to solve this, so I come to humbly ask for advice.
I have a JS var that gets strings similar to this:
"value=8kadctgwqqe0&value=8kaczvfgoyrs&value=8kwkgz2ysm1i"
it doesn't have a fixed lenght. it may return 1 or 20 values, there's no way to know.
As you can see, it returns a big sequence of values that are always between "value=" and "&", except for the last one, that has no "&" in the end.
I need to parse that, and get this in the end:
8kadctgwqqe0
8kaczvfgoyrs
8kwkgz2ysm1i
I don't even know where to start...
Thanks a lot!
personally I would avoid regex for this.
if your string is always in the value=SOMETHING&value=SOMETHINGELSE format use this
string.split("value=").join("").split("&")
according to How to replace all occurrences of a string in JavaScript? its actually quicker to use this over regex also
var string = "value=8kadctgwqqe0&value=8kaczvfgoyrs&value=8kwkgz2ysm1i"
var yourArray = string.split("value=").join("").split("&")
["8kadctgwqqe0", "8kaczvfgoyrs", "8kwkgz2ysm1i"]
You may need to add some tests to ensure the correct format of string if you are not sure about it.
The following RegExp/.filter() should do it:
'value=8kadctgwqqe0&value=8kaczvfgoyrs&value=8kwkgz2ysm1i'.match(/([^=&]+)/g).filter(function(a){return a!='value'});
The .match() will grab the separate query values (and the key names). In order to deal with this, we use .filter() to remove the value results. This will leave it with the correct results.
Sometimes it's better to use regular JavaScript string manipulation (even though this looks like the worst answer)
.filter() is awesome, more info on it here
You can use the following regex:
/(?:^|&)value=([^&]*)/g
regex101 demo
The matches are then stored in the first capture group.
You can now access the capture groups using this method:
var myRegexp = /(?:^|&)value=([^&]*)/g;
var myString = "value=8kadctgwqqe0&value=8kaczvfgoyrs&value=8kwkgz2ysm1i";
match = myRegexp.exec(myString);
while (match != null) {
console.log(match[1]);
match = myRegexp.exec(myString);
}
If you are interested in the keys as well, you can use:
/(?:^|&)([^=&]*)=([^&]*)/g
In that case for each iteration, the key can be found in the first match, and the value in the second one:
var myRegexp = /(?:^|&)([^&=]*)=([^&]*)/g;
var myString = "value=8kadctgwqqe0&value=8kaczvfgoyrs&value=8kwkgz2ysm1i";
match = myRegexp.exec(myString);
while (match != null) {
console.log(match[1]); //key
console.log(match[2]); //value
match = myRegexp.exec(myString);
}
This works as well:
str.match(/value=([^&]*)/g).join("").split('value=').splice(1)

Regex equivalent to str.substr(0, str.indexOf('foo'))

Given this string:
var str = 'A1=B2;C3,D0*E9+F6-';
I would like to retrieve the substring that goes from the beginning of the string up to 'D0*' (excluding), in this case:
'A1=B2;C3,'
I know how to achieve this using the combination of the substr and indexOf methods:
str.substr(0, str.indexOf('D0*'))
Live demo: http://jsfiddle.net/simevidas/XSu22/
However, this is obviously not the best solution since it contains a redundancy (the str name has to be written twice). This redundancy can be avoided by using the match method together with a regular expression that captures the substring:
str.match(/???/)[1]
Which regular expression literal do we have to pass into match to ensure that the correct substring is returned?
My guess is this: /(.*)D0\*/ (and that works), but my experience with regular expressions is rather limited, so I'm going to need a confirmation...
Try this:
/(.*?)D0\*/.exec(str)[1]
Or:
str.match(/(.*?)D0\*/)[1]
DEMO HERE
? directly following a quantifier makes the quantifier non-greedy (makes it match minimum instead of maximum of the interval defined).
Here's where that's from
/^(.+?)D0\*/
Try it here: http://rubular.com/r/TNTizJLSn9
/^.*(?=D0\*)/
more text to hit character limit...
You can do a number-group, like your example.
/^(.*?)foo/
It mean somethink like:
Store all in group, from start (the 0)
Stop, but don't store on found foo (the indexOf)
After that, you need match and get
'hello foo bar foo bar'.match(/^(.*?)foo/)[1]; // will return "hello "
It mean that will work on str variable and get the first (and unique) number-group existent. The [0] instead [1] mean that will get all matched code.
Bye :)

Using javascript regexp to find the first AND longest match

I have a RegExp like the following simplified example:
var exp = /he|hell/;
When I run it on a string it will give me the first match, fx:
var str = "hello world";
var match = exp.exec(str);
// match contains ["he"];
I want the first and longest possible match,
and by that i mean sorted by index, then length.
Since the expression is combined from an array of RegExp's, I am looking for a way to find the longest match without having to rewrite the regular expression.
Is that even possible?
If it isn't, I am looking for a way to easily analyze the expression, and arrange it in the proper order. But I can't figure out how since the expressions could be a lot more complex, fx:
var exp = /h..|hel*/
How about /hell|he/ ?
All regex implementations I know of will (try to) match characters/patterns from left to right and terminate whenever they find an over-all match.
In other words: if you want to make sure you get the longest possible match, you'll need to try all your patterns (separately), store all matches and then get the longest match from all possible matches.
You can do it. It's explained here:
http://www.regular-expressions.info/alternation.html
(In summary, change the operand order or group with question mark the second part of the search.)
You cannot do "longest match" (or anything involving counting, minus look-aheads) with regular expressions.
Your best bet is to find all matches, and simply compare the lengths in the program.
I don't know if this is what you're looking for (Considering this question is almost 8 years old...), but here's my grain of salt:
(Switching the he for hell will perform the search based on the biggest first)
var exp = /hell|he/;
var str = "hello world";
var match = exp.exec(str);
if(match)
{
match.sort(function(a, b){return b.length - a.length;});
console.log(match[0]);
}
Where match[0] is going to be the longest of all the strings matched.

Categories

Resources