Solved

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

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

Suggested Solutions

Title # Comments Views Activity
how can i make 2 columns? 4 42
How to resize a div in html 3 39
JQuery Syntax... 4 38
Need a regex pattern to find a string between two strings 12 13
As most anyone who uses or has come across them can attest to, regular expressions (regex) are a complicated bit of magic. Packed so succinctly within their cryptic syntax lies a great deal of power. It's not the "take over the world" kind of power,…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
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…

821 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