Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting content between curly braces in JavaScript with regex

I am trying to get content between curly braces with JavaScript. I found this thread: Regex to get string between curly braces "{I want what's between the curly braces}"

But I do not know how to apply a regex like /\{([^}]+)\}/

I have tried string.replace('/\{([^}]+)\}/','');, however this does not work.

like image 972
alex Avatar asked Apr 02 '11 03:04

alex


People also ask

How do you match curly braces with regular expressions?

To match literal curly braces, you have to escape them with \ . However, Apex Code uses \ as an escape, too, so you have to "escape the escape". You'll need to do this almost every time you want to use any sort of special characters in your regexp literally, which will happen more frequently than not.

What does regex match return?

The Match(String) method returns the first substring that matches a regular expression pattern in an input string. For information about the language elements used to build a regular expression pattern, see Regular Expression Language - Quick Reference.

What is written inside the curly brackets on a function in JavaScript?

It's an object literal. Show activity on this post. Basically the curly braces {} are the another way for creating objects in javascript. This is equivalent to the "new Object()" syntax.

How do you use parentheses in regex?

Use Parentheses for Grouping and Capturing. By placing part of a regular expression inside round brackets or parentheses, you can group that part of the regular expression together. This allows you to apply a quantifier to the entire group or to restrict alternation to part of the regex.


4 Answers

Here's an example of use:

var found = [],          // an array to collect the strings that are found
    rxp = /{([^}]+)}/g,
    str = "a {string} with {curly} braces",
    curMatch;

while( curMatch = rxp.exec( str ) ) {
    found.push( curMatch[1] );
}

console.log( found );    // ["string", "curly"]
like image 98
meouw Avatar answered Oct 08 '22 15:10

meouw


Like this?

var x="omg {wtf} bbq";

alert(x.match(/{([^}]+)}/));
like image 20
Khez Avatar answered Oct 08 '22 13:10

Khez


Try this

/[^{\}]+(?=})/g

for example

Welcome to RegExr v2.1 by {gskinner.com}, {ssd.sd} hosted by Media Temple!

it will return 'gskinner.com', 'ssd.sd'

like image 45
Mohamed Abo Elmagd Avatar answered Oct 08 '22 13:10

Mohamed Abo Elmagd


"{I want what's between the curly braces}".replace(/{(.*)}/, "$1");

this should work, cheers.

Note: you'll "get everything" in the braces, even if it's an empty string.

Updated: If you are going to match the first character which in the middle of a string, use "match":

"how are you {I want what's between the curly braces} words".match(/{(.*)}/)[1];

You can just do:

console.log("how are you {I want what's between the curly braces} words".match(/{(.*)}/));

then you'll see a list of the match items, exploit them to whatever you want.

Gory details: http://www.w3schools.com/jsref/jsref_obj_regexp.asp

like image 34
Linmic Avatar answered Oct 08 '22 13:10

Linmic