Solved

How to pull out the text written between symbols for each line from textarea?

Posted on 2015-01-03
5
186 Views
Last Modified: 2015-01-04
How to pull out the text written between (any) symbols for each line from textarea using javascript.
I was thinking to use split() but didn't use it before enough. It needs to be specific enought to create array.

Example of textarea:
Mike Smith June 22 (happy)
Liza Gray May 01 (sad)
Peter Dec (lucky)

Result:
Array (happy,sad,lucky)

It's easier for me to do it excel but I need it to be done with javascript.
Hope you have a simple solution, Thanks.
0
Comment
Question by:SSupreme
  • 3
5 Comments
 
LVL 50

Assisted Solution

by:Steve Bink
Steve Bink earned 100 total points
ID: 40530332
Here's an example from javascript console:
> a='Mike Smith June 22 (happy)'
< "Mike Smith June 22 (happy)"
> a.substr(a.indexOf('(')+1).replace(')','')
< "happy"

Open in new window

Does this work for you?
0
 
LVL 25

Expert Comment

by:Kyle Hamilton
ID: 40530365
console log it, or push to a new array. 'group' is the found element

var txt = "Mike Smith June 22 (happy) Liza Gray May 01 (sad) Peter Dec (lucky)";
txt.replace(/\(([^)]+)\)/g, function(m, group) { 
    console.log(group)
})

Open in new window


outputs:
 happy
 sad
 lucky
0
 
LVL 25

Accepted Solution

by:
Kyle Hamilton earned 350 total points
ID: 40530424
Using split performs marginally better, but looks uglier. Not sure it's worth the perf boost. depends how many operations you plan on doing.

var newTxt = txt.split('(');
for (var i = 1; i < newTxt.length; i++) {
        console.log(newTxt[i].split(')')[0]);
}

Open in new window


here's a perf test:
http://jsperf.com/stringvsregex/11
0
 
LVL 44

Assisted Solution

by:Rainer Jeschor
Rainer Jeschor earned 50 total points
ID: 40530501
Hi,
the easiest one would be the usage of a regular expression:
/\((.*?)\)/g

Open in new window


You can test it here:
https://www.regex101.com/#javascript

Just enter the above regex, set the global flag (the g at the end) and add your sample content - voila.

HTH
Rainer
0
 
LVL 25

Expert Comment

by:Kyle Hamilton
ID: 40530548
Rainer,

Simply setting the global flag you will end up with the parenthesis included in the results. see jsfiddle: http://jsfiddle.net/em1mq9b5/

You need the call back function to extract the group (ie, no parenthesis). See above solution:

http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28590197.html#a40530365

or any other solution that loops over the resulting arrays:
 more examples:
http://jsfiddle.net/em1mq9b5/2/
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Why do we like using grid based layouts in website design? Let's look at the live examples of websites and compare them to grid based WordPress themes.
I've been asked to discuss some of the UX activities that I'm using with my team. Here I will share some details about how we approach UX projects.
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

705 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now