-4

I am new to regular expressions. Can anyone suggest me the equivalent regular expression for below strings. I need to validate these in input text-box.

"Start<b>Middle</b>End"

or

"Start<b>End</b>"

or

"<b>Start</b>End"

or

"StartMiddleEnd"
Ankur
  • 730
  • 7
  • 28
  • 4
    Do you want to check if there is a bold tag in a given string or what? – VisioN Mar 24 '14 at 12:43
  • yes i have to check for the bold is there or not, and reject all other html tags for the time being. – Ankur Mar 24 '14 at 12:46
  • Keep in mind, that there are 2 HTML interpretations (tags) for the `bold` tag, the legacy `` and the new one `` – Bud Damyanov Mar 24 '14 at 12:47
  • So do you want to match any html tag, or just ``. And what do you mean by "reject"? Remove all tags? Or just flag the user for invalid input or something? – aliteralmind Mar 24 '14 at 12:48
  • Yeah, I know that but currently I am focusing only tag – Ankur Mar 24 '14 at 12:48
  • Also why do you think you need some [broken regex](http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags) for the job? – PeeHaa Mar 24 '14 at 12:49
  • @PeeHaa can you suggest me some other way at the client side?? – Ankur Mar 24 '14 at 12:51
  • if you all remember that Orkut(one of the old social networking site) which supported html tags in our posts. I am almost looking for the same in this scenario, that if a user gives a tag in the input string then the respective string gets highlighted. but no other html tag is allowed in the input string – Ankur Mar 24 '14 at 12:59
  • I'm not entirely sure what you want, but if you can use PHP, strip_tags or htmlspecialchars will remove all tags from given $ (well, htmlspecialchars will literally write 'Hello') – Déjà vu Mar 24 '14 at 12:59
  • 2
    Rule 1: never parse HTML with RegEx. Rule2: if you want to use RegEx to parse HTML, see rule 1. – freefaller Mar 24 '14 at 13:12

2 Answers2

2

Really you should use the DOM for this.

First convert the strings into DOM objects and once you have done that check whether it contains a b tag:

function hasBTag(html) {
    var parser = new DOMParser();

    var node = parser.parseFromString(html, "text/html");
    var allNodes = node.getElementsByTagName('*');

    for (var i = -1, l = allNodes.length; ++i < l;) {
        if (allNodes[i].nodeName === 'B' ) {
            return true;
        }
    }

    return false;
}

(function() {
    var html = [
        '"Start<b>Middle</b>End"',
        '"Start<b>End</b>"',
        '"<b>Start</b>End"',
        '"StartMiddleEnd"'
    ];

    for (var i = 0, l = html.length; i < l; i++) {
        if (hasBTag(html[i])) console.log(html[i] + ' haz b tag');
    }
}());

Le demo: http://jsfiddle.net/mZu7Z/

Trying to parse HTML using regex is almost always a terrible idea.

PeeHaa
  • 71,436
  • 58
  • 190
  • 262
0

dude you are not that clear. just to check if bold tag is there or not you dont need regular expression. If you want to check any pattern rather than just a fixed text then regular expressions mainly come into picture. Assuming you dont want to replace but only test wether bold tag is present on not just use indexOf.

var strToTest = "Hi, my name is <b>Ankur</b>";
if(strToTest.indexOf("<b>")!=-1 || strToTest.indexOf("</b>")!=-1)
{
   console.log("String includes bold tag");
}

Few people say that you need to escape <,> and /. however in this scenario it is not needed.

If you want to use regular expression to check both start and end bold tag then use this

/\</?b>/gi

This tests for both and

wallop
  • 2,510
  • 1
  • 22
  • 39
  • Thanks, for the reply. But may be this can help you to enhance your answer, if you all remember that Orkut(one of the old social networking site) which supported html tags in our posts. I am almost looking for the same in this scenario, that if a user gives a tag in the input string then the respective string gets highlighted. but no other html tag is allowed in the input string. – Ankur Mar 24 '14 at 13:02
  • I dont know why using regex is "almost always a terrible idea with html" when the html we are talking about is in terms of a string, but for the above scenario you have mentioned i will get u another regEx. Since he has asked for regex i am giving him regex, i have no clue y somebody voted down my previous answer. An explanation with voting down will always help. – wallop Mar 25 '14 at 05:20
  • Using the below regex you can check if a particular string containes xxxx where xxxx is some string value and also you can get the value of xxxx so that you can manipulate or highlight it. /.*\(.*)\<\?b\>/gi So when you are testing u can use \1 to get access to the value inside and when you are replacing you can use $1 to get access to the value inside . – wallop Mar 25 '14 at 05:22