Click here to Skip to main content
15,887,135 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I have the following string:
"President: John Doe - Vice-President: Harry Arthur Blair"

I need to match the names of President and Vice-President (in this case: "John Doe" and "Harry Arthur Blair"). Names are always after colon.

How can I accomplish this?

What I have tried:

JavaScript
string.match(/: \w+/g);

But it matches the first word only after each colon, and the colon itself (in the example above: ": John" and ": Harry").

A solution could be:
JavaScript
[pres, vice] = string.replace(/\w+: /g, "").split(" - ");


But I would like to use just "match".
Posted
Updated 24-Nov-23 6:10am
v2

Try this

Notice that there is a space after the w.
Quote:
(?<=:) [\w ]+


This is a really good site for learning RregEx.
regex101: build, test, and debug regex[^]
 
Share this answer
 
v2
Comments
LB2371 24-Nov-23 10:59am    
I've tried your regex on regextester.com - It says: "ERROR: Lookbehind [i.e. "(?<="] is not supported in Javascript"
Mike Hankey 24-Nov-23 11:18am    
Sorry didn't see the javascript

(?:) [\w ]+
LB2371 24-Nov-23 11:22am    
This one matches " Vice", too.
Mike Hankey 24-Nov-23 11:39am    
(?: [\w ]+ )
LB2371 24-Nov-23 12:11pm    
Did you test it before posting?
If you want to use 'Match' you should look at the following code-
JavaScript
const inputString = 'President: John Doe - Vice-President: Harry Arthur Blair';

// Using match with a regular expression to capture names after colons
const regex = /(?:President|Vice-President): ([^-]+)/g;
const matches = inputString.match(regex);

if (matches) {
  // Now, 'matches' array contains the names of President and Vice-President
  const [president, vicePresident] = matches.map(match => match.split(': ')[1].trim());

  console.log('President:', president);
  console.log('Vice-President:', vicePresident);
} else {
  console.log('No matches found.');
}


The working fiddle here - Using match[^]
 
Share this answer
 
v2
Comments
LB2371 24-Nov-23 15:57pm    
Your solution is a little... intricate. Anyway, it doesn't work correctly: it matches "Harry Arthur" instead of "Harry Arthur Blair" (as it should be).
Andre Oosthuizen 25-Nov-23 6:56am    
I am not sure what you mean by 'intricate...'. I have adjusted my code above to read and log full names. See the fiddle I have given that works fine. In my edit, the regular expression '/(?:President|Vice-President): ([^-]+)/g' captures any character except for the the dash, after the colon until the dash or the end of the string. The '.trim() method' is used to remove any leading or trailing spaces.
Quote:
Javascript regex - how to match words after each colon in a string?

Bewaren a RegEx is about describing what to match and also what "no match" surroubs it.
your match starts after a colon and stops before the '-' or end if string.
You have to describe it all.
Nota: In French, "Jean-Yves" is a first compound name, since the "-" is also the end of match, you have to assume that your president can't have such name.

This avoid matching the space after 'Doe'
: ([\w]+( [\w]+)+)


Just a few interesting links to help building and debugging RegEx.
Here is a link to RegEx documentation:
perlre - perldoc.perl.org[^]
Here is links to tools to help build RegEx and debug them:
.NET Regex Tester - Regex Storm[^]
Expresso Regular Expression Tool[^]
RegExr: Learn, Build, & Test RegEx[^]
Online regex tester and debugger: PHP, PCRE, Python, Golang and JavaScript[^]
This one show you the RegEx as a nice graph which is really helpful to understand what is doing a RegEx: Debuggex: Online visual regex tester. JavaScript, Python, and PCRE.[^]
This site also show the Regex in a nice graph but can't test what match the RegEx: Regexper[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900