I have a string with type, the expected results are
input = "[Peter Jane Minesotta <[email protected]>]"
output
Fname = "Peter"
SecondAndRemainingNames = "Jane Minesotta"
email = "[email protected]"
input = "[Peter <[email protected]>]"
output
Fname = "Peter"
SecondAndRemainingNames = ""
email = "[email protected]
I need to extract using regex
I have tried with
input.match(/\w/gim)
You can use
const rx = /\[(\S+)(?:\s+(.*?))?\s+<([^<>]+)>]/
const strings = ['[Peter Jane Minesotta <[email protected]>]','[Peter <[email protected]>]'];
for (const s of strings) {
const [_, Fname, SecondAndRemainingNames, email] = s.match(rx);
console.log([Fname, SecondAndRemainingNames, email]);
}
See the regex demo.
Details
\[
- a [
char(\S+)
- Group 1: one or more non-whitespace chars (to stay within [...]
, you may use [^\s[\]]+
instead)(?:\s+(.*?))?
- an optional string of 1+ whitespaces followed with Group 2 capturing any zero or more chars other than line break chars as few as possible (replace .*?
with [^[\]]*?
if you want to stay within [...]
)\s+
- one or more whitespaces<([^<>]+)>
- >
, Group 3: one or more chars other than <
and >
, then >
]
- a ]
char.You can use 3 different regex in order to simplify the problem. Also, you can rely on the structure of the string:
const input1 = "[Peter Jane Minesotta <[email protected]>]"
const input2 = "[Peter <[email protected]>]"
function getFName(input) {
const name = input.match(/(?<=\[)\w+/);
return name ? name[0] : '';
}
function getSNames(input) {
const names = input.match(/(?<!\[)(?<=\s)\w+(?=\s)/g);
return names ? names.join(' ') : '';
}
function getEmail(input) {
const mail = input.match(/(?<=<)(?:\w|\.|@)+(?=>])/);
return mail ? mail[0] : '';
}
const x = {
name: getFName(input1),
otherNames: getSNames(input1),
mail: getEmail(input1)
};
console.log(x);
const y = {
name: getFName(input2),
otherNames: getSNames(input2),
mail: getEmail(input2)
};
console.log(y);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With