?
Solved

JavaScript/REGEX: First word in a string that contains an underscore

Posted on 2012-08-16
9
Medium Priority
?
451 Views
Last Modified: 2012-08-17
Using JavaScript, how can I get the first word in a string that contains an underscore?

alert( getF('hello world abc_xyz test test_123') ); // should alert "abc_xyz"

function getF (str) {
 return str.replace(' selected','');
}

Open in new window

0
Comment
Question by:hankknight
  • 4
  • 3
  • 2
9 Comments
 
LVL 35

Accepted Solution

by:
Terry Woods earned 2000 total points
ID: 38303225
Use match pattern /\b\w+?_\w+\b/

or a replace:
alert( getF('hello world abc_xyz test test_123') ); // should alert "abc_xyz"

function getF (str) {
 return str.replace(/.*?\b(\w+?_\w+)\b.*/,'$1');
}

Open in new window

0
 
LVL 35

Expert Comment

by:Terry Woods
ID: 38303231
Let me know if that works for you.

I've assumed you won't have a _ character at the start or end of the "word" you're targeting.
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 38303752
@TerryAtOpus
I've assumed you won't have a _ character at the start or end of the "word" you're targeting.
Have you?    ; )
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 60

Expert Comment

by:Julian Hansen
ID: 38303826
Alternative
function getF(str)
{
   var match = str.match(/\w+\_\w+/);
   return (match != null)?match[0]:'not found';
}

Open in new window

Change the w+ to w* depending on whether you want to enforce can / can't start / end with '_'
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 38303848
@julianH
Change the w+ to w* depending on whether you want to enforce can / can't start / end with '_'
As I teased Terry, "\w" encompasses underscore, so changing the + to * (and vice versa) has no bearing on whether or not the "word" can start with an underscore. Your pattern begins and ends with "\w"--any matching word can start or end with an underscore by default  = )
0
 
LVL 60

Expert Comment

by:Julian Hansen
ID: 38303926
I know - but it works :) I found out by accident - haven't quite figured out exactly why yet but if you change the \w+\_\w+ to \w*\_\w+ the it allows '_this' whereas it does not in the former.

Same for\w+\_\w* matches 'this_'
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 38303947
Never mind, you're correct. (I really should stop making comments during insomnia fits.) My apologies  = )
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 38303953
The point I was making was that either pattern would match "this__" (two underscores at the end).
0
 
LVL 60

Expert Comment

by:Julian Hansen
ID: 38303967
Never mind, you're correct. (I really should stop making comments during insomnia fits.) My apologies  = )

You too? - I thought only I did that :) Not that it came across like that ...

You are correct on the double underscore - tradeoff between simplicty and absolute correctness I suppose. I would tend to go with the cater for all eventualities in which case my solution would not pass muster - but if the author is coninced that the data won't contain a double underscore then he should be ok.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question