Does anyone know how I can find the sub-string "<br/>" in a string and check if there’s a space before or after it? I’ve been using this to check if the string contains the sub-string:
if (str.indexOf('<br/>') !== -1) {
}
But how would I go about checking before or after it for a space? Thanks for the help!
Edit: this solution works if you also care about the index of of the
<br/>even when there are no spaces. If you don’t care about the<br/>when there are no spaces, then @David’s solution is better (although note that\bmatches any word boundary, so you may want to make it stricter, depending on your needs).Another edit: I just realized several of the regex solutions offered will only work if there is a space before AND after. Here’s an example that will work with a space before or after or on both sides: