Solved

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

Posted on 2014-03-28
1
572 Views
Last Modified: 2014-03-28
Using JavaScript and Regular Expressions, how can I get the content from a string between  
{[English]}
and
{[/English]}
?

This does not work, probably because of escaping related issues:
var data = '{[Spanish]}Hola{[/Spanish]}{[English]}Hello{[/English]}{[Italian]}Ciao{[/Italian]}';
alert(  data.match('{[English]}(.*){[/English]}')[1] );

Open in new window

0
Comment
Question by:hankknight
1 Comment
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
ID: 39962092
You need to use a regex in match not a string
I mean : new RegExp or /your regex here/

Test page : http://jsfiddle.net/c8SUZ/

var data = '{[Spanish]}Hola{[/Spanish]}{[English]}Hello{[/English]}{[Italian]}Ciao{[/Italian]}';
alert(  data.match(/{\[English\]}(.*){\[\/English\]}/)[1] );

Open in new window


You need also to escape : [] and /
The escaping char is : \
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

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 …
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…
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)

760 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

19 Experts available now in Live!

Get 1:1 Help Now