What regex pattern do I need for this?

前端 未结 5 678
不思量自难忘°
不思量自难忘° 2020-12-22 09:48

I need a regex (to work in PHP) to replace American English words in HTML with British English words. So color would be replaced by colour, meters by metres and so on [I kno

5条回答
  •  -上瘾入骨i
    2020-12-22 10:01

    The second problem is easier - you want to replace when there are word boundaries around the word: http://www.regular-expressions.info/wordboundaries.html -- this will make sure you don't replace the meter in Brammeter.

    The first problem is much harder. You don't want to replace words inside HTML entities - nothing between <> characters. So, your match must make sure that you last saw > or nothing, but never just <. This is either hard, and requires some combination of lookahead/lookbehind assertions, or just plain impossible with regular expressions.

    a script implementing a state machine would work much better here.

提交回复
热议问题