?
Solved

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

Posted on 2014-03-30
5
Medium Priority
?
474 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
5 Comments
 

Assisted Solution

by:szgal
szgal earned 200 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 1800 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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

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-…
A while back, I ran into a situation where I was trying to use the calculated columns feature in SharePoint 2013 to do some simple math using values in two lists. Between certain data types not being accessible, and also with trying to make a one to…
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…
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)

718 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