Solved

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

Posted on 2014-03-30
5
463 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
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…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

744 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

15 Experts available now in Live!

Get 1:1 Help Now