Getting string parameters using regex - javascript

I'm trying to get a regex that can extract data from
BAYARPLN ke 116160029354, SUKSES. Hrg: 84.822. SN: TGK IMUM M SAMIN/R1/450/MAR,APR/Rp.89222/Adm6000/977-1071/047421CA414149E5CEC5. Saldo: 7
and I want to find this value like this...
977-1071
I tried to using parameter regex link this
"/(Adm6000)([^\7]+)/"
But I cant find the string regex 977-1071. Can I ask for help for this?

Did you try like this? see regex https://regex101.com/r/tccJ42/1
const regex = /\d+\-\d+/g; //use \d{3}\-\d{4} if you've digit limit
const str = `BAYARPLN ke 116160029354, SUKSES. Hrg: 84.822. SN: TGK IMUM M SAMIN/R1/450/MAR,APR/Rp.89222/Adm6000/977-1071/047421CA414149E5CEC5. Saldo: 7`;
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++;
}
// The result can be accessed through the `m`-variable.
m.forEach((match, groupIndex) => {
console.log(`Found match, group ${groupIndex}: ${match}`);
});
}

If you want to match 977-1071 after /Adm6000/ you could first match /Adm6000/ and then capture in a group not a forward slash one or more times ([^/]+)
\/Adm6000\/([^/]+)
Your value 977-1071 will be in captured group 1:
const regex = /Adm6000\/([^/]+)/;
const str = `BAYARPLN ke 116160029354, SUKSES. Hrg: 84.822. SN: TGK IMUM M SAMIN/R1/450/MAR,APR/Rp.89222/Adm6000/977-1071/047421CA414149E5CEC5. Saldo: 7`;
let match = regex.exec(str);
console.log(match[1]);

Related

Regex to find the best matching subsetof a word

I have a list of coma separated words like cooler,bestwishes,congrat. I want to use regex to find the best matching word in this list. e.g Congratulations or Congrats matches congrat in the above list.
I have tried the regex below but it only works if the word in regex is the subset.
const regex = /[^,]*congratulation[^,]*/g;
const str = `this,cart,open,best-wishes,congrat`;
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++;
}
// The result can be accessed through the `m`-variable.
m.forEach((match, groupIndex) => {
console.log(`Found match, group ${groupIndex}: ${match}`);
});
}
Is this possible using regex ?
Instead of searching for the target word's substring in a list of words you can search list of words in target word. That will reduce complexity and make it easier.
let words = ["cool","bestwishes","congrat","greatjob","welldone","kudos","thumbsup","keeprocking","rock","congrats"];
let word = "keeprockingbuddy";
let match = getMatchingWords(words,word);
console.log(match); // ["keeprocking", "rock"]
match = getMatchingWords(words,"Congratulations");
console.log(match); // ["congrat"]
function getMatchingWords(words,target){
let ans = [];
words.forEach((w)=>{
let found = target.match(new RegExp(w,"i"));
if(found){
ans.push(w);
}
})
ans = ans.length ? ans:"not found";
return ans;
}
Hope it answers your question.

Regex for parsing multiple conditions for groups

I am trying to split string in 3 different parts with regex.
I can only get function parameters from string but i also want to other parts of the string
const regex = /(\(.*?\))+/g;
const sampleString = 'collection.products(take:12|skip:16)';
const result = sampleString.match(regex)
It gives me (take:12|skip:16)
But i also want to get collection and products
Expected result in match
collection
products
take:12|skip:16
Here, we can alter two expressions together:
(\w+)|\((.+?)\)
which group #1 would capture our desired words (\w+) and group #2 would capture the desired output in the brackets.
const regex = /(\w+)|\((.+?)\)/gm;
const str = `collection.products(take:12|skip:16)`;
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++;
}
// The result can be accessed through the `m`-variable.
m.forEach((match, groupIndex) => {
console.log(`Found match, group ${groupIndex}: ${match}`);
});
}
Demo
RegEx Circuit
jex.im visualizes regular expressions:
You can split the string on . and (\(.*?\))+ and then use reduce to get values in desired format
const sampleString = 'collection.products(take:12|skip:16)';
const result = sampleString.split(/\.|(\(.*?\))+/).reduce((op,inp) => {
if(inp){
inp = inp.replace(/[)(]+/g,'')
op.push(inp)
}
return op
},[])
console.log(result)
This splits on what you want.
const sampleString = 'collection.products(take:12|skip:16)';
const result = sampleString.split(/[.()]*([^.()]+)[.()]*/).filter(function (el) {return el != "";});
console.log(result)

how to match '\u' codes for all the emojis with regexp?

I want to find match all the strings which will have emojis in the form of \u codes. I'm trying with below regexp but is not working.
/([\uE000-\uF8FF]|\uD83C[\uDC00-\uDFFF]|\uD83D[\uDC00-\uDFFF]|[\u2694-\u2697]|\uD83E[\uDD10-\uDD5D])/g
But, it is not detecting. I want to match and get
\ud83d\ude04\ud83d\ude04\ud83d\ude04\ud83d\ude04\ud83d\ude04\ud83d\ude04
these type of characters.
Well if you want to match emojis in the format \uXXXX using Regex, you can use this Regex:
/\\u[a-z0-9]{4}/gi
This is a simple Demo:
const regex = /\\u[a-z0-9]{4}/gi;
const str = `This is a pragraph \\ud83d having some emojis like these ones:
\\ude04
\\ud83d
\\ude04
Have you seen them?
\\ud83d\\ude04
Great!
`;
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++;
}
// The result can be accessed through the `m`-variable.
m.forEach((match, groupIndex) => {
console.log(`Found match, group ${groupIndex}: ${match}`);
});
}
The regex you wrote won't work because you were not escaping the \.

Parse pipes when in JavaScript

I would like to split a string where the delimiter is a pipe.
But the pipe can be escaped and not splitted than.
example:
'aa|bb|cc'.split(/*??*/) // ['aa','bb','cc']
'aa\|aa|bb|cc|dd\|dd|ee'.split(/*??*/) // ['aa|aa', 'bb', 'cc', 'dd|dd', 'ee']
I try this, but it not work in javascript: (?<!\\)[\|]
Try this:
console.log('aa|bb|cc'.split('|'));
console.log('aa\|aa|bb|cc|dd\|dd|ee'.split('|'));
I assume that you want to skip splitting on escaped pipes. Use match instead:
console.log(
'aa\\|aa|bb|cc|dd\\|dd|ee'.match(/[^\\|]*(?:\\.[^\\|]*)*/g).filter(Boolean)
);
Hi the Regex I created
https://www.regexpal.com/index.php?fam=100132
What you need to do is concact the matches in an array
the code generated looks like this...
const regex = /(([^\\\|]+)\\?\|\2)|(\w+)/g;
const str = `aa\\|aa|bb|cc|dd\\|dd|ee`;
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++;
}
// The result can be accessed through the `m`-variable.
m.forEach((match, groupIndex) => {
console.log(`Found match, group ${groupIndex}: ${match}`);
});
}
Hope this help you.

Best way to get the word right before a certain word in javascript

I have the following string
this is the string and THIS is the word I want
I've tried using a regex for this:
var to_search = "is"
var regex = "/\S+(?="+to_search+")/g";
var matches = string.match(regex);
And I wanted matches to contain "THIS" (word that comes after the second if) but it does not seem to be working
Any idea? Thanks
regex101.com is a really great site to test your regex and it even generates the code for you.
const regex = /\bis.*(this)/gi;
const str = `this is the string and THIS is the word I want`;
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++;
}
// The result can be accessed through the `m`-variable.
m.forEach((match, groupIndex) => {
console.log(`Found match, group ${groupIndex}: ${match}`);
});
}
First you have to double backslashes when using the string form of regexes.
Second, you forgot whitespace in your pattern:
var regex = new RegExp("\\S+\\s+(?="+to_search+")", "g");

Categories

Resources