Solved

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

Posted on 2015-01-03
5
193 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

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!

Question has a verified solution.

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

When it comes to write a Context Sensitive Help (an online help that is obtained from a specific point in state of software to provide help with that state) ,  first we need to make the file that contains all topics, which are given exclusive IDs. …
Color can increase conversions, create feelings of warmth or even incite people to get behind a cause. If you want your website to really impact site visitors, then it is vital to consider the impact color has on them.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
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.

733 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