# RegEx: Starts With WORDA But Doesn't Contain WORDB

**URL:** <https://discourse.nodered.org/t/regex-starts-with-worda-but-doesnt-contain-wordb/41983>\
**Category:** General\
**Created:** [4 March 2021 03:23 UTC](https://discourse.nodered.org/t/regex-starts-with-worda-but-doesnt-contain-wordb/41983 "2021-03-04T03:23:54Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![plfinch](https://avatars.discourse-cdn.com/v4/letter/p/65b543/32.png) [@plfinch](https://discourse.nodered.org/u/plfinch)\
**Post date:** [4 March 2021 03:23 UTC](https://discourse.nodered.org/t/regex-starts-with-worda-but-doesnt-contain-wordb/41983/1 "2021-03-04T03:23:54Z")

</div>

I need to validate some msg.payload data in a Switch Node. To be valid, the data must start with WORDA but not contain WORDB. The data spans multiple lines (has newlines) and WORDB, if present and invalidating the data, could be anywhere on any line.

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [4 March 2021 05:07 UTC](https://discourse.nodered.org/t/regex-starts-with-worda-but-doesnt-contain-wordb/41983/2 "2021-03-04T05:07:39Z")

</div>

Must it be regex? A simple function would do the job using calls to the string functions `startsWith` & `contains`

```auto
if ( msg.payload.startsWith("WORDA") && !msg.payload.contains("WORDB")) {
  msg.valid = true;
}

```

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [4 March 2021 09:51 UTC](https://discourse.nodered.org/t/regex-starts-with-worda-but-doesnt-contain-wordb/41983/3 "2021-03-04T09:51:59Z")

</div>

Or in a Switch node first check for contains WORDB and send that to output 1, then check for starts with WORDA and send that to output 2. Set the switch to Stop after first match and connect the next node to output 2. Leave o/p 1 disconnected.

---

<div class="post-metadata">

**Author:** ![plfinch](https://avatars.discourse-cdn.com/v4/letter/p/65b543/32.png) [@plfinch](https://discourse.nodered.org/u/plfinch)\
**Post date:** [7 March 2021 17:08 UTC](https://discourse.nodered.org/t/regex-starts-with-worda-but-doesnt-contain-wordb/41983/4 "2021-03-07T17:08:20Z")

</div>

Yes, I prefer a regex. Seems like this should be possible but I struggle some with regex... There are multiple ways to solve the problem but I am looking specifically for this regex pattern for multiple similar scenarios.

---

<div class="post-metadata">

**Author:** ![plfinch](https://avatars.discourse-cdn.com/v4/letter/p/65b543/32.png) [@plfinch](https://discourse.nodered.org/u/plfinch)\
**Post date:** [7 March 2021 17:16 UTC](https://discourse.nodered.org/t/regex-starts-with-worda-but-doesnt-contain-wordb/41983/5 "2021-03-07T17:16:10Z")

</div>

Yes, that is how I originally implemented it to get things going when I couldn’t figure out the proper regex. From a programming style standpoint, I prefer not to have switch output nodes without connections if they can be avoided. I have also encountered other scenarios where an “a and not b” regex would be useful.

I googled specific to just node-red initially but just now tried ‘regex for a and not b’ and found several examples I need to try.

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [7 March 2021 17:17 UTC](https://discourse.nodered.org/t/regex-starts-with-worda-but-doesnt-contain-wordb/41983/6 "2021-03-07T17:17:32Z")

</div>

Regex's aren't good at negative assertions. You will be much better off with Steve's suggestion.

---

<div class="post-metadata">

**Author:** ![plfinch](https://avatars.discourse-cdn.com/v4/letter/p/65b543/32.png) [@plfinch](https://discourse.nodered.org/u/plfinch)\
**Post date:** [7 March 2021 17:31 UTC](https://discourse.nodered.org/t/regex-starts-with-worda-but-doesnt-contain-wordb/41983/7 "2021-03-07T17:31:23Z")

</div>

I need to do more testing but this appears to work:  
`^(?!.*WORDB)^WORDA `

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [21 March 2021 17:31 UTC](https://discourse.nodered.org/t/regex-starts-with-worda-but-doesnt-contain-wordb/41983/8 "2021-03-21T17:31:59Z")

</div>

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
