Solved

JavaScript/REGEX: Get content from between two points in string using function

Posted on 2014-03-30
5
464 Views
Last Modified: 2014-03-30
I want to be able to use this in a function.  Now the "English" results are always returned.

function getStuff(data, language) {
 return data.match(/{\[English\]}(.*){\[\/English\]}/)[1];
}

alert( getStuff('{[Spanish]}Hola{[/Spanish]}{[English]}Hello{[/English]}{[Italian]}Ciao{[/Italian]}', 'English') );
// Should alert "Hello"

alert( getStuff('{[Spanish]}Hola{[/Spanish]}{[English]}Hello{[/English]}{[Italian]}Ciao{[/Italian]}', 'Italian') );
// Should alert "Ciao"

alert( getStuff('{[Spanish]}Hola{[/Spanish]}{[English]}Hello{[/English]}{[Italian]}Ciao{[/Italian]}', 'XYZ') );
// Should alert ""
// (Empty with no error because there is no match for XYZ.

Open in new window

0
Comment
Question by:hankknight
  • 3
5 Comments
 

Assisted Solution

by:szgal
szgal earned 50 total points
ID: 39965221
With the RegExp class constructor, you can dynamically create regexp objects, so your function will look like this:
function getStuff(data, language) {
 return data.match(new RegExp('/{\[' + language + '\]}(.*){\[\/' + language + '\]}/'))[1];
}

Open in new window

0
 
LVL 24

Expert Comment

by:mankowitz
ID: 39965223
function getStuff(data, language) {
    var re = new RegExp("{\\\[" + language + "\\\]}(.*){\\\[\\\/" + language + "\\\]}");
 return re.exec(data)[1];
}
0
 
LVL 24

Expert Comment

by:mankowitz
ID: 39965226
Ahhh. you beat me to it. Anyway, I like szgal's approach better. It's cleaner.
0
 
LVL 16

Author Comment

by:hankknight
ID: 39965244
Both ideas posted so far return errors for the last text!

Please test this:
alert( getStuff('{[Spanish]}Hola{[/Spanish]}{[English]}Hello{[/English]}{[Italian]}Ciao{[/Italian]}', 'XYZ') );
// Should alert ""
// (Empty with no error because there is no match for XYZ.

Open in new window

0
 
LVL 24

Accepted Solution

by:
mankowitz earned 450 total points
ID: 39965247
function getStuff(data, language) {
    var re = new RegExp("{\\\[" + language + "\\\]}(.*){\\\[\\\/" + language + "\\\]}");
    var ret=re.exec(data);
    return ret && ret[1] || "";
}
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

Do you hate spam? I do, and I am willing to bet you do as well. I often wonder, though, "if people hate spam so much, why do they still post their email addresses on the web?" I'm not talking about a plain-text posting here. I am referring to the fa…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

932 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

8 Experts available now in Live!

Get 1:1 Help Now