Are the angle brackets (< or >) special in a regular expression?

I am trying to get a regex expression to accept < and > as my outside delimiters to grab all the content in between them.

so content like such

< tfdsfa  >

should be grabbed.

Do I have to escape the < and > characters or something?

Regex generated by my script:

/<[^(>)]*>/g

Code from file:

data.method.highlight = function() {
    var x = data.syntax,
        text = data.$.span.html();
    for (var i=0, len = x.length; i < len; i++) {
        var rx;
        if (x[i].range) {
            rx = new RegExp(x[i].tag[0] + "[^(" + x[i].tag[1] + ")]*" + x[i].tag[1], "g");
            console.log(rx);
        }
        else {
            var temprx = x[i].tag[0];
            for (var z = 1; z < x[i].tag.length; z++) {
                temprx += "|" + x[i].tag[z];
            }
            rx = new RegExp(temprx, "g");
        }
        text = text.replace(rx,function (match) {
            console.log("looping - range");
            return '<span class="' + x[i].class.default + '">' + match + '</span>';
        });
        data.$.span.html(text);
    }
};

Answers:

Answer

Neither < nor > are metacharacters inside a regular expression.

This works for me:

'<foo> and <bar>'.match(/<[^>]*>/g); // ["<foo>", "<bar>"]
Answer

May be you have problem because you trying to insert result into HTML and browser 'thinks' that it is a not valid HTML tag, like <blablabla>.

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.