Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex to find ampersands without spaces on both sides

Tags:

regex

php

I'm just trying to figure out the regex to find any ampersands that aren't immediately preceded and followed by a space. For example, it would find "asdf&asdf" and "asdf& asdf" but not "asdf & asdf"

This will be used in a preg_replace to add spaces before and after. (If you're wondering, the problem is that I'm stuck with a WYSIWYG that has a bug where it strips spaces on both sides of ampersands, and I need to manually add them back after the fact). Thoughts?

like image 738
Mike Crittenden Avatar asked Dec 17 '22 03:12

Mike Crittenden


1 Answers

(\S&\S|\s&\S|\S&\s)
  • Non whitespace char, followed by & and another non space char
    OR
  • Whitespace followed by &, followed by non space char
    OR
  • Non whitespace char followed by &, followed by another whitespace char
like image 134
Amarghosh Avatar answered Jan 08 '23 12:01

Amarghosh