Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2015-01-03
5
Medium Priority
?
200 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 51

Assisted Solution

by:Steve Bink
Steve Bink earned 400 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 1400 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 200 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Australian government abolished Visa 457 earlier this April and this article describes how this decision might affect Australian IT scene and IT experts.
When the s#!t hits the fan, you don’t have time to look up who’s on call, draft emails, call collaborators, or send text messages. An instant chat window is definitely the way to go, especially one like HipChat. HipChat is a true business app. An…
The viewer will learn how to count occurrences of each item in an array.
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.
Suggested Courses

926 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