# RegEx to assert HTML in a response body

**URL:** <https://community.postman.com/t/regex-to-assert-html-in-a-response-body/33969>\
**Category:** Help Hub\
**Tags:** html, regex\
**Created:** [March 28, 2022, 4:42pm UTC](https://community.postman.com/t/regex-to-assert-html-in-a-response-body/33969 "2022-03-28T16:42:45Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![w4dd325](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/w4dd325/32/12436_2.png) [@w4dd325](https://community.postman.com/u/w4dd325)\
**Post date:** [March 28, 2022, 4:42pm UTC](https://community.postman.com/t/regex-to-assert-html-in-a-response-body/33969/1 "2022-03-28T16:42:45Z")

</div>

I have this line return within HTML in a response body;  
`<script type="text/javascript" src="/main.fed3fbbceb96763d430c.chunk.js"></script>`

The string is a hash of letters and numbers and I’m trying to assert that the value in “src” is

> /main.REGEX.chunk.js

I have been reading other posts and the best I have come up with is this;

```auto
const response = pm.response.text();
let my_regex = new RegExp(pm.globals.get('regEx'));
pm.test("Body matches string", function () {
    pm.expect(response).to.include(`src="/main.${my_regex}.chunk.js"`);
});

```

However, it still doesn’t work, can anyone help me alter this to work properly?  
Any guidance would be appreciated.

---

<div class="post-metadata">

**Author:** ![ianthepostmanaut](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/ianthepostmanaut/32/11109_2.png) [@ianthepostmanaut](https://community.postman.com/u/ianthepostmanaut)\
**Post date:** [March 28, 2022, 11:26pm UTC](https://community.postman.com/t/regex-to-assert-html-in-a-response-body/33969/2 "2022-03-28T23:26:15Z")

</div>

I’m not sure from your original post whether you already know the hexadecimal string `fed3ff..` or if that’s what you’re trying to find.

If it’s the first case, where you already know the pattern, then this should work:

```auto
// save response text
let text = pm.response.text()

// grab your pattern from global variables
let myPattern = pm.globals.get('regEx')

pm.test("body contains regex", function() {
  // detect if the pattern exists in the text
  pm.expect(text).to.include(`main.${myPattern}.chunk.js`)
})

```

If you’re trying to FIND the pattern based on what’s in the text, something like this should work:

```auto
let text = `... <script type="text/javascript" src="/main.fed3fbbceb96763d430c.chunk.js"></script> ...`

myRe = new RegExp('main\.([^.]*)\.chunk\.js', 'g');
myArray = myRe.exec(text)
pm.test("body contains regex and saves it", function() {
  pm.expect(myArray.length).to.be.gte(2)
  pm.globals.set('regEx', myArray[1])
})

```

---

<div class="post-metadata">

**Author:** ![w4dd325](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/w4dd325/32/12436_2.png) [@w4dd325](https://community.postman.com/u/w4dd325)\
**Post date:** [March 29, 2022, 10:18am UTC](https://community.postman.com/t/regex-to-assert-html-in-a-response-body/33969/3 "2022-03-29T10:18:32Z")

</div>

Thank you for your help @ianthepostmanaut

It was the second option that I was aiming for. I have taken your code and tweaked it to work with a try-catch so that I can run multiple checks for different strings/regex.  
(There’s only 1 at the moment, but the important thing is it works!)

Here is the code for anyone else that encounters the same…

```auto
const text = pm.response.text();
myRe = new RegExp('main\.([^.]*)\.chunk\.js', 'g');
myArray = myRe.exec(text);
let count = 0;

pm.test("Check for RegEx", () => {
    try {
        pm.expect(myArray.length).to.be.gte(2);
        pm.test("Body contains regex");
    } catch (e) {
        pm.test("Body doesn't contain regex", () => {
            throw new Error(e.message)
        }), count++;
    }
    if (count > 0) {
        console.error("There are failures within the above checks, please check...");
        pm.expect.fail("There are " + count + " failures within the above checks, please check...");
    }
});

```

[Info about .exec](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/exec)  
If the match succeeds, the exec() method returns an array (with extra properties index, input, and if the d flag is set, indices; see below) and updates the lastIndex property of the regular expression object. The returned array has the matched text as the first item, and then one item for each parenthetical capture group of the matched text. If the match fails, the exec() method returns null, and sets lastIndex to 0.

---

<div class="post-metadata">

**Author:** ![w4dd325](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/w4dd325/32/12436_2.png) [@w4dd325](https://community.postman.com/u/w4dd325)\
**Post date:** [March 29, 2022, 8:30pm UTC](https://community.postman.com/t/regex-to-assert-html-in-a-response-body/33969/4 "2022-03-29T20:30:17Z")

</div>

Hi again @ianthepostmanaut

I’ve been playing around with this code and it’s working great, but I was wondering if you could clarify what the `'g'` part of the code this line does?  
`myRe = new RegExp('main\.([^.]*)\.chunk\.js', 'g');`

I have seen examples that use ‘i’ too … but I can’t seem to find a description of what it does.

Cheers

---

<div class="post-metadata">

**Author:** ![kevinc-postman](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/kevinc-postman/32/12774_2.png) [@kevinc-postman](https://community.postman.com/u/kevinc-postman)\
**Post date:** [March 29, 2022, 8:39pm UTC](https://community.postman.com/t/regex-to-assert-html-in-a-response-body/33969/5 "2022-03-29T20:39:20Z")

</div>

Hey!

So the `g` pretty much just asks it to match against all possible substrings in a string.

Here’s an [MDN link](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/global) that might help explain better 😄

Usually when I write/test regex, I use something like [https://regex101.com/](https://regex101.com/) which gives some excellent explanations for what things do.

---

<div class="post-metadata">

**Author:** ![w4dd325](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/w4dd325/32/12436_2.png) [@w4dd325](https://community.postman.com/u/w4dd325)\
**Post date:** [March 29, 2022, 8:54pm UTC](https://community.postman.com/t/regex-to-assert-html-in-a-response-body/33969/6 "2022-03-29T20:54:00Z")

</div>

Cheers @kevinc-postman, the description on there;

> The " `g` " flag indicates that the regular expression should be tested against all possible matches in a string

Was just what I was looking for!!  
(ironically I was on that website I just hadn’t spotted that!)

Thank you!

---

<div class="post-metadata">

**Author:** ![ianthepostmanaut](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/ianthepostmanaut/32/11109_2.png) [@ianthepostmanaut](https://community.postman.com/u/ianthepostmanaut)\
**Post date:** [March 29, 2022, 9:06pm UTC](https://community.postman.com/t/regex-to-assert-html-in-a-response-body/33969/7 "2022-03-29T21:06:40Z")

</div>

With regular expressions you can set flags to run once, or run “globally” (aka, find multiple matches). I tend to use “global” with most of what I do with regex, so it’s just kind of a default for me 🙂

You’re welcome to remove the ‘g’ and that will only find the first match.
