0

I have this string:

var x = '<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title></title>
    </head>
    <body>
        <a href="{url}">This is an example of url with the URL keyword</a>
        <p>This is another keyword: {link1}</p>
    </body>
</html>'

I want to make a array with all {chr} matches. A rule like this:

Each word with 10 characters max length wrapped by { and } in x, add in array

I think this can be done using regex but i'm new to regex and don't know how to.

Any ideas?

4

2 回答 2

6

这是一种方法:

var arr = x.match(/{\w{1,10}}/g);
// ["{url}", "{link1}"]
于 2013-07-23T13:34:06.687 回答
1

试试这个

var results = x.match(/\{\w{1,10}\}/g)

告诉我它是否有效

于 2013-07-23T13:34:48.223 回答