Solved

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

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Change javascript css 1 19
Excel Hyperlink Question 4 30
Scientific articles behind a login? 6 9
push logos in footer up higher 5 10
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
This video teaches users how to migrate an existing Wordpress website to a new domain.
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…

910 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

22 Experts available now in Live!

Get 1:1 Help Now