Solved

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

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

In this article, we'll look how to sort an Array in JavaScript, including the more advanced techniques of sorting a collection of records either ascending or descending on two or more fields. Basic Sorting of Arrays First, let's look at the …
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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…
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…

809 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