Solved

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

Posted on 2015-01-03
5
190 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
An enjoyable and seamless user experience can go a long way on an eCommerce site. While a cohesive layout and engaging copy play roles in creating a positive user experience, some sites neglect aspects that seem marginal but in actuality prove very …
This video teaches users how to migrate an existing Wordpress website to a new domain.
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…

832 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