0

I have the below html as a text string

<p>I'll write a H1 below</p>
<h1>Hi I'm H1</h1>
<p>Last p</p>

What i want to do is see if 'I'm H1' exists in the h1 tag - curious as to what the regex above would be so that it returns true

P.S: Even if it contained <h1>I'm H1 buddy</h1> it should return true so essentially in a h1 tag, if it contains a particular string, in the above case 'I'm H1', it should output true

5
  • 1
    If it is the plain regex you want, then here it is, \<h1\>.(I'm H1).\</h1\>. It will work if the whole thing is a string. Commented Jun 1, 2015 at 13:22
  • 1
    stackoverflow.com/questions/1732348/… Commented Jun 1, 2015 at 13:23
  • @VinodKumar Did you try that? Doesn't work for the example provided (there's no character after the "I'm H1" to match the . and only matches one character before. Could be SO removing '*' ? Commented Jun 1, 2015 at 13:25
  • I am sorry, my mistake. I did not try. This should work \<h1\>.*?(I'm H1).*?\<\/h1\> Commented Jun 1, 2015 at 13:28
  • Not sure why this was tagged with javascript, but that's why you're getting javascript answers... Commented Jun 1, 2015 at 13:30

5 Answers 5

1

The following regex should work:

<h1>.*I'm H1.*<\/h1>

That's <h1> followed by optional text (.*), followed by "I'm H1", followed by more optional text (.*), followed by </h1>.

Demonstration on Online regex tester.

Sign up to request clarification or add additional context in comments.

2 Comments

thanks, this is the simplest way to do it and it just works - one question regarding escaping - the keyword i'm trying to test with is a variable - say 'var keyword' with a particular value and the following does not work: new RegExp("/<h1>.*?" + "http redirect 302" + ".*?<\/h1>/gi").test(plainHtml)
pretty sure i'm not escaping it properly but don't know what the right way to do it is
1

It is a way to solve your problem with Regex.

var re = /<h1>.*?\sI'm\sH1.*?<\/h1>/; 
var str = '<p>I\'ll write a H1 below</p>\n<h1>Hi I\'m H1 buddy</h1>\n<p>Last p</p>';
var m;
 
if ((m = re.exec(str)) !== null) {
    if (m.index === re.lastIndex) {
        re.lastIndex++;
    }
    // View your result using the m-variable.
    // eg m[0] etc.
}

document.getElementById("results").innerHTML = m != null
<div id="results"></div>

1 Comment

And what happen if the OP add buggy?
0

You can try this:

/(I'm H1)/.test("Hi I'm H1") // return true
/(I'm H1)/.test("Hi I'm H1 buddy") // return true

Comments

0
<h1>.*?I'm\sH1.*?</h1>
  • .*? any or none characters with minimum match (so doesn't match multiple H1s)
  • \s for any whitespace
  • I'm\sH1 literal

Edit: no need to escape < and >

Comments

-1

Is there a reason you need a regex?

if (document.getElementsByTagName('h1')[0].innerHTML.indexOf("Hi I'm H1") != -1) 
{ 

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.