Regex - Extract the first word from a string - javascript

I want to parse the text below:
Recipient Name: Tracy Chan SKU: 103990
I want to extract "Tracy" only, the first word after "Recipient Name:" as the first name
So I got as far as /(?<=Recipient Name: )(.*)(?= SKU)/gm but it only gives me "Tracy Chan".... Using the ECMA option in Regex101...
Appreciate any help on this.
Thanks, Tracy

Use \S+ to match a sequence of non-whitespace characters, instead of .*, to get one word.
let text = 'Recipient Name: Tracy Chan SKU: 103990';
let match = text.match(/(?<=Recipient Name: )\S+/);
console.log(match[0]);

To extract "Tracy" only, you can use the following regular expression:
/(?<=Recipient Name: )(\S+)/gm
This will match the first word (i.e., the first sequence of non-whitespace characters) after the "Recipient Name:" string.
The \S character class matches any non-whitespace character, and the + quantifier specifies that the preceding pattern should be matched one or more times until the first whitespace.
a working example:
const input = "Recipient Name: Tracy Chan SKU: 103990";
const regex = /(?<=Recipient Name: )(\S+)/gm;
const matches = regex.exec(input);
console.log(matches[0]); // "Tracy"
Update: based on your comments below, you also need to extract the last name from your string value.
I would suggest to either use the original regex written in your question, or use this one, in order to extract both Tracy and Chan, then you can use the javascript split method` to split the string into an array with all the extracted names.
consider the following example:
const input = "Recipient Name: Tracy Chan SKU: 103990";
const regex = /(?<=Recipient Name: )([^ ]+)\s([^ ]+)/gm;
const allMatches = input.match(regex);
let resultArray = allMatches[0].split(' ');
console.log('firstName: '+ resultArray[0]); // "Tracy"
console.log('lastName: '+ resultArray[1]); // "Chan"

(?<=Recipient Name: )([^ ]+) .*(?= SKU)
This Works

Related

How to split a string based on a regex pattern with conditions (JavaScript)

I am trying to split a string so that I can separate it depending on a pattern. I'm having trouble getting the correct regex pattern to do so. I also need to insert the results into an array of objects. Perhaps by using a regex pattern, the string can be split into a resulting array object to achieve the objective. Note that the regex pattern must not discriminate between - or --. Or is there any better way to do this?
I tried using string split() method, but to no avail. I am trying to achieve the result below:
const example1 = `--filename test_layer_123.png`;
const example2 = `--code 1 --level critical -info "This is some info"`;
const result1 = [{ name: "--filename", value: "test_layer_123.png" }];
const result2 = [
{ name: "--code", value: "1" },
{ name: "--level", value: "critical" },
{ name: "-info", value: "This is some info" },
];
If you really want to use Regex to solve this.
Try this Pattern /((?:--|-)\w+)\s+"?([^-"]+)"?/g
Code example:
function matchAllCommands(text, pattern){
let new_array = [];
let matches = text.matchAll(pattern);
for (const match of matches){
new_array.push({name: match.groups.name, value: match.groups.value});
}
return new_array;
}
let RegexPattern = /(?<name>(?:--|-)\w+)\s+"?(?<value>[^-"]+)"?/g;
let text = '--code 1 --level critical -info "This is some info"';
console.log(matchAllCommands(text, RegexPattern));
Here is a solution that splits the argument string using a positive lookahead, and creates the array of key & value pairs using a map:
function getArgs(str) {
return str.split(/(?= --?\w+ )/).map(str => {
let m = str.match(/^ ?([^ ]+) (.*)$/);
return {
name: m[1],
value: m[2].replace(/^"(.*)"$/, '$1')
};
});
}
[
'--filename test_layer_123.png', // example1
'--code 1 --level critical -info "This is some info"' // example2
].forEach(str => {
var result = getArgs(str);
console.log(JSON.stringify(result, null, ' '));
});
Positive lookahead regex for split:
(?= -- positive lookahead start
--?\w+ -- expect space, 1 or 2 dashes, 1+ word chars, a space
) -- positive lookahead end
Match regex in map:
^ -- anchor at start of string
? -- optional space
([^ ]+) -- capture group 1: capture everything to next space
-- space
(.*) -- capture group 2: capture everything that's left
$ -- anchor at end of string

How to get specific text from a string in regex

I have a string from which I need to extract specific texts
let str = 'id = "Test This is" id ="second" abc 123 id ="third-123"';
let res = str.match(/[^id ="\[](.*)[^\]]/g);
console.log(res);
I want the texts in ids only ['Test This is','second','third-123']
But I am getting [ 'Test This is" id ="second" abc 123 id ="third-123"' ]
The whole text after first id which I don't want.I need help with the pattern.
Your pattern uses a negated character class where you exclude matching the listed individual characters, and also exclude matching [ and ] which are not present in the example data.
That way you match the first char T in the string with [^id ="\[] and match the last char ; in the string with [^\]] and the .* captures all in between.
I would suggest using a negated character class to exclude matching the " instead:;
\bid\s*=\s*"([^"]*)"
Regex demo
let str = 'id = "Test This is" id ="second" abc 123 id ="third-123"';
let res = str.matchAll(/\bid\s*=\s*"([^"]*)"/g);
console.log(Array.from(str.matchAll(/\bid\s*=\s*"([^"]*)"/g), m => m[1]));
You can simplify this down to a non-greedy regular expression indepedent of where the quotes fall in the string:
let str = 'id = "Test This is" id ="second" abc 123 id ="third-123"';
let res = str.match(/".*?"/g);
console.log(res);

How to get 2 Characters after space using regex or without using regex from a string

I need to get intials only 2 characters after spaces, here is the sample
const string = "John Peter Don";
const result = string.match(/\b(\w)/g).join('');
console.log(result)// JPD->> i want only JP
One regex approach would be to capture all leading capital letters using match(). Then, slice off the first two elements and join together to form a string output.
var string = "John Peter Don";
var initials = string.match(/\b[A-Z]/g).slice(0, 2).join("");
console.log(initials);
Implementing what Wiktor Stribiżew suggested in the comments
const str = "John Peter Don";
let array = str.split(" ");
console.log(array[0][0], array[1][0]);
console.log("In one string: ", array[0][0] + array[1][0]);

Why is this regex matching also words within a non-capturing group?

I have this string (notice the multi-line syntax):
var str = ` Number One: Get this
Number Two: And this`;
And I want a regex that returns (with match):
[str, 'Get this', 'And this']
So I tried str.match(/Number (?:One|Two): (.*)/g);, but that's returning:
["Number One: Get this", "Number Two: And this"]
There can be any whitespace/line-breaks before any "Number" word.
Why doesn't it return only what is inside of the capturing group? Am I misundersating something? And how can I achieve the desired result?
Per the MDN documentation for String.match:
If the regular expression includes the g flag, the method returns an Array containing all matched substrings rather than match objects. Captured groups are not returned. If there were no matches, the method returns null.
(emphasis mine).
So, what you want is not possible.
The same page adds:
if you want to obtain capture groups and the global flag is set, you need to use RegExp.exec() instead.
so if you're willing to give on using match, you can write your own function that repeatedly applies the regex, gets the captured substrings, and builds an array.
Or, for your specific case, you could write something like this:
var these = str.split(/(?:^|\n)\s*Number (?:One|Two): /);
these[0] = str;
Replace and store the result in a new string, like this:
var str = ` Number One: Get this
Number Two: And this`;
var output = str.replace(/Number (?:One|Two): (.*)/g, "$1");
console.log(output);
which outputs:
Get this
And this
If you want the match array like you requested, you can try this:
var getMatch = function(string, split, regex) {
var match = string.replace(regex, "$1" + split);
match = match.split(split);
match = match.reverse();
match.push(string);
match = match.reverse();
match.pop();
return match;
}
var str = ` Number One: Get this
Number Two: And this`;
var regex = /Number (?:One|Two): (.*)/g;
var match = getMatch(str, "#!SPLIT!#", regex);
console.log(match);
which displays the array as desired:
[ ' Number One: Get this\n Number Two: And this',
' Get this',
'\n And this' ]
Where split (here #!SPLIT!#) should be a unique string to split the matches. Note that this only works for single groups. For multi groups add a variable indicating the number of groups and add a for loop constructing "$1 $2 $3 $4 ..." + split.
Try
var str = " Number One: Get this\
Number Two: And this";
// `/\w+\s+\w+(?=\s|$)/g` match one or more alphanumeric characters ,
// followed by one or more space characters ,
// followed by one or more alphanumeric characters ,
// if following space or end of input , set `g` flag
// return `res` array `["Get this", "And this"]`
var res = str.match(/\w+\s+\w+(?=\s|$)/g);
document.write(JSON.stringify(res));

regex help in extracting values from a string

I have a string in javascript like
"some text #[14cd3:+Seldum Kype] things are going good for #[7f8ef3:+Kerry Williams] so its ok"
From this i want to extract the name and id for the 2 people. so data like -
[ { id: 14cd3, name : Seldum Kype},
{ id: 7f8ef3, name : Kerry Williams} ]
how can u use regex to extract this?
please help
var text = "some text #[14cd3:+Seldum Kype] things are going " +
"good for #[7f8ef3:+Kerry Williams] so its ok"
var data = text.match(/#\[.+?\]/g).map(function(m) {
var match = m.substring(2, m.length - 1).split(':+');
return {id: match[0], name: match[1]};
})
// => [ { id: '14cd3', name: 'Seldum Kype' },
// { id: '7f8ef3', name: 'Kerry Williams' } ]
// For demo
document.getElementById('output').innerText = JSON.stringify(data);
<pre id="output"></pre>
Get the id from Group index 1 and name from group index 2.
#\[([a-z\d]+):\+([^\[\]]+)\]
DEMO
Explanation:
# Matches a literal # symbol.
\[ Matches a literal [ symbol.
([a-z\d]+) Captures one or more chars lowercase alphabets or digits.
:\+ Matches :+ literally.
([^\[\]]+) Captures any character but not of [ or ] one or more times.
\] A literal ] symbol.
Try the following, the key is to properly escape reserved special symbols:
#\[([\d\w]+):\+([\s\w]+)\]

Categories

Resources