Javascript regex to parse string - javascript

I have the following strings :
id,square,vegetable.garden:id<>cell:gardenId
id,square,vegetable.garden:id<>cell:gardenId?id=10
id,square,vegetable.garden:id<>cell:gardenId#toto
id,square,vegetable.garden:id<>cell:gardenId^titi
id,square,vegetable.garden:id<>cell:gardenId?id=10#toto^titi
id,square,vegetable.garden:id<>cell:gardenId#toto^titi
With JS I want to match all parameters. I have the regex
/(\S*)\.(\S*)([\?#\^])(\S*)([\?#\^])(\S*)([\?#\^])(\S*)/g
But this regex matching and catching parameters for the 5th string only. Do you have an solution to match all the strings ?
Thanks for your help.
Sorry if it's not clear.
I have this
id,square,vegetable.garden:id<>cell:gardenId?id=10#toto^titi
And I want to split parameters to have :
group1 = id,square,vegetable
group2 = garden:id<>cell:gardenId
group3 = ?
group4 = id=10
group5 = #
group6 = toto
group7 = ^
group8 = titi
The previous regex working. But for other string this regex not working because I can have differents order for #, ^ or ? separator and they can be optionals.
Online RegEx with examples here.
It's more clear ?

Assuming that you only have 3 pieces maximum that it can extend to:
Here's a regex that should work for you:
([^\s.]*)\.([^?#^\s]+)(?:([?#^])([^?\s#^]+))?(?:([?#^])([^?\s#^]+))?(?:([?#^])([^?\s#^]+))?
It matches all the example strings that you gave and seems to match fairly quickly as well.
The repeating bit (?:([?#^])([^?\s#^]+))?matches optionally if there is a ?, #, or ^ followed by all the characters that aren't one of them or a whitespace.
https://regex101.com/r/MRzMdK/2

It's a little unclear what you want to do, but you could try something like this to use a regex to see if a string contains another string:
const str1 = 'id,square,vegetable.garden:id<>cell:gardenId'
const str2 = 'id,square,vegetable.garden:id<>cell:gardenId?id=10'
const str3 = 'id,square,vegetable.garden:id<>cell:gardenId#toto'
const str4 = 'id,square,vegetable.garden:id<>cell:gardenId^titi'
const str5 = 'id,square,vegetable.garden:id<>cell:gardenId?id=10#toto^titi'
const str6 = 'id,square,vegetable.garden:id<>cell:gardenId#toto^titi'
const regex = new RegExp(str1, "ig")
const res = str2.match(regex)
console.log(res)

Related

How can I replace all duplicated paths of a url with a JS Regex

For the following URL:
https://www.google.es/test/test/hello/world
I want to replace all the occurrences of "/test/", and its important that it "test" starts with and ends with a "/".
I tried with:
let url = "https://www.google.es/test/test/hello/world"
url.replace(/\/test\//g, "/");
But it returns:
'https://www.google.es/test/hello/world'
It doesn't replace the second "/test/"
Any clues on how I could do this with a regex?
I basically want to replace the content that lives inside the dashes, but not the dashes themselves.
Something like this would work:
/(\/[^\/]+)(?:\1)+\//g
( - open capture group #1
\/ - find a literal slash
[^\/]+ - capture at least one non-slash char
) - close capture group #1
(?:\1)+ - repeat capture group #1 one or more times
\/ - ensure a closing slash
/g - global modifier
https://regex101.com/r/NgJA3X/1
var regex = /(\/[^\/]+)(?:\1)+\//g;
var str = `https://www.google.es/test/test/hello/world
https://www.google.es/test/test/test/test/hello/world
https://www.google.es/test/test/hello/test/hello/hello/world
https://www.google.es/test/hello/world/world
https://www.google.es/test/hello/helloworld/world`;
var subst = ``;
// The substituted value will be contained in the result variable
var result = str.replace(regex, subst);
console.log(result);
You can do this with a regular expression, but it sounds like your intent is to replace only individual parts of the pathname component of a URL.
A URL has other components (such as the fragment identifier) which could contain the pattern that you describe, and handling that distinction with a regular expression is more challenging.
The URL class is designed to help solve problems just like this, and you can replace just the path parts using a functional technique like this:
function replacePathParts (url, targetStr, replaceStr = '') {
url = new URL(url);
const updatedPathname = url.pathname
.split('/')
.map(str => str === targetStr ? replaceStr : str)
.filter(Boolean)
.join('/');
url.pathname = updatedPathname;
return url.href;
}
const inputUrl = 'https://www.google.es/test/test/hello/world';
const result1 = replacePathParts(inputUrl, 'test');
console.log(result1); // "https://www.google.es/hello/world"
const result2 = replacePathParts(inputUrl, 'test', 'message');
console.log(result2); // "https://www.google.es/message/message/hello/world"
Based on conversation in comment, you can use this solution:
let url = "https://www.google.es/test/test/hello/world"
console.log( url.replace(/(?:\/test)+(?=\/)/g, "/WORKS") );
//=> https://www.google.es/WORKS/hello/world
RegEx Breakdown:
(?:\/test)+: Match 1+ repeats of text /test
(?=\/): Lookahead to assert that we have a / ahead

Javascript get only matched text in regex

I have string like below
BANKNIFTY-13-FEB-2020-31200-ce
I want to convert the string to 13-FEB-31200-ce
so I tried below code
str.match(/(.*)-(?:.*)-(?:.*)-(.*)-(?:.*)-(?:.*)/g)
But its returning whole string
Two capture groups is probably the way to go. Now you have two options to use it. One is match which requires you to put the two pieces together
var str = 'BANKNIFTY-13-FEB-2020-31200-ce'
var match = str.match(/[^-]+-(\d{2}-[A-Z]{3}-)\d{4}-(.*)/)
// just reference the two groups
console.log(`${match[1]}${match[2]}`)
// or you can remove the match and join the remaining
match.shift()
console.log(match.join(''))
Or just string replace which you do the concatenation of the two capture groups in one line.
var str = 'BANKNIFTY-13-FEB-2020-31200-ce'
var match = str.replace(/[^-]+-(\d{2}-[A-Z]{3}-)\d{4}-(.*)/, '$1$2')
console.log(match)
Regex doesn't seem to be the most appropriate tool here. Why not use simple .split?
let str = 'BANKNIFTY-13-FEB-2020-31200-ce';
let splits = str.split('-');
let out = [splits[1], splits[2], splits[4], splits[5]].join('-');
console.log(out);
If you really want to use regexp,
let str = 'BANKNIFTY-13-FEB-2020-31200-ce';
let splits = str.match(/[^-]+/g);
let out = [splits[1], splits[2], splits[4], splits[5]].join('-');
console.log(out);
I would not use Regex at all if you know exact positions. Using regex is expensive and should be done differently if there is way. (https://blog.codinghorror.com/regular-expressions-now-you-have-two-problems/)
const strArr = "BANKNIFTY-13-FEB-2020-31200-ce".split("-"); // creates array
strArr.splice(0,1); // remove first item
strArr.splice(2,1); // remove 2020
const finalStr = strArr.join("-");
If the pattern doesn't need to be too specific.
Then just keep it simple and only capture what's needed.
Then glue the captured groups together.
let str = 'BANKNIFTY-13-FEB-2020-31200-ce';
let m = str.match(/^\w+-(\d{1,2}-[A-Z]{3})-\d+-(.*)$/)
let result = m ? m[1]+'-'+m[2] : undefined;
console.log(result);
In this regex, ^ is the start of the string and $ the end of the string.
You can have something like this by capturing groups with regex:
const regex = /(\d{2}\-\w{3})(\-\d{4})(\-\d{5}\-\w{2})/
const text = "BANKNIFTY-13-FEB-2020-31200-ce"
const [, a, b, c] = text.match(regex);
console.log(`${a}${c}`)

Test for specific number of words to match, with a known separator with Regex in Js

i'm trying to check wether a string matches a set of values and they are seperated by a ; It needs to have ; as a separator.
I go this new RegExp(/\b(Segunda|Terça|Quarta|Quinta|Sexta|Sábado|Domingo)\b/, 'gi').test(str)
If i pass:
'Segunda;Terça', true.
'Segundaaa', false.
'Segunda;Terçaa', true.. Why is it true? how can i avoid this?
Thanks in advance.
[EDIT] code:
const WEEK_DAYS_GROUP_REGEX = /\b(Segunda|Terça|Quarta|Quinta|Sexta|Sábado|Domingo)\b/;
const res = new RegExp(WEEK_DAYS_GROUP_REGEX, 'i').test('Segunda;Terçaa');
console.log(res) // gives true
The /\b(Segunda|Terça|Quarta|Quinta|Sexta|Sábado|Domingo)\b/ pattern with gi modifiers matches any of the alternatives as a whole word, it does not guarantee that the whole string consists of these values only, let alone the ; delimiter.
You may use
^(<ALTERNATIONS>)(?:;(<ALTERNATIONS>))*$
See the pattern demo.
In JS, you do not need to use that long pattern, you may build the pattern dynamically:
const strs = ["Segunda;Terça", "Segundaaa", "Segunda;Terçaa"];
const vals = "Segunda|Terça|Quarta|Quinta|Sexta|Sábado|Domingo";
let rx = new RegExp("^(?:" + vals + ")(?:;(?:" + vals + "))*$", "i");
console.log(rx);
for (let s of strs) {
console.log(s,"=>",rx.test(s));
}
Note that the non-capturing groups (?:...) are preferred when there is no need extracting submatches, group values.

regex match everything between two different characters

I want to match a string between (but not including) these two characters: ? and &
Example string:
localhost/path/doc.html?970441179&token=specialtoken&actionurl=/portletaction/01654/0112
So from the above I want to match the string 970441179
var str = "?samplestring&";
var patt = /[?]([^&]*)[&]/g;
var res = patt.exec(str)[1];
'res' is your desired result.
Try this regex (\d+)(?=&):
var str = "localhost/path/doc.html?970441179&token=specialtoken&actionurl=/portletaction/01654/0112";
console.log(str.match(/(\d+)(?=&)/g));
Note that it will work just for that specific case.

Javascript regex to bring back all symbol matches?

I need a javascript regex object that brings back any matches of symbols in a string,
take for example the following string:
input = !"£$[]{}%^&*:#\~#';/.,<>\|¬`
then the following code:
input.match(regExObj,"g");
would return an array of matches:
[[,!,",£,$,%,^,&,*,:,#,~,#,',;,/,.,,,<,>,\,|,¬,`,]]
I have tried the following with no luck.
match(/[U+0021-U+0027]/g);
and I cannot use the following because I need to allow none ascii chars, for example Chinese characters.
[^0-9a-zA-Z\s]
var re = /[!"\[\]{}%^&*:#~#';/.<>\\|`]/g;
var matches = [];
var someString = "aejih!\"£$[]{}%^&*:#\~#';/.,<>\\|¬`oejtoj%";
while(match = re.exec(someString)) {
matches.push(match[1]);
}
Getting
['!','"','[',']','{','}','%','^','&','*',':','#','~','#',''',';','/','.','<','>','\','|','`','%]
What about
/[!"£$\[\]{}%^&*:#\\~#';\/.,<>|¬`]/g
?

Categories

Resources