When using Anqicms for content operation, we often encounter situations where we need to replace text content in bulk. Anqicms provides convenientreplaceA filter can be implemented at template rendering time. However, a common issue is how to ensure that thisreplaceThe filter only replaces whole words, not partial string matches?For example, we want to replace all 'Go' with 'Golang', but we don't want 'Good' to become 'Golangod'.

To deeply understand this problem, we first need to understandreplaceThe way a filter works.

UnderstandingreplaceThe principle of the filter.

The template engine of Anqi CMS supports syntax similar to Django, wherereplaceThe filter is used to perform simple search and replace operations on strings. Its basic usage is{{ obj|replace:"旧字符串,新字符串" }}.

This filter is designed with simplicity and efficiency in mind, it performssubstring replacementThis means that as long as the 'old string' is found in the original string, it will be replaced with the 'new string', regardless of whether it constitutes a complete word.

For example, you have a variable{{ content }}Its value isGo语言非常棒,我在AnQiCMS中使用Go。这是一个GoodCMS。If you want to replace allGowithGolangyou might write it like this:

{{ content|replace:"Go,Golang" }}

At first glance, it seems that this can solve the problem. But the actual output will be:

Golang语言非常棒,我在AnQiCMS中使用Golang。这是一个GolangodCMS。

It is not difficult to find that, in addition to the expected replacement,GoodCMSofGopart of it has also been replaced, generatingGolangodCMSThis is obviously not the effect we want. That isreplaceThe limitation of substring matching in filters.

The limitation of replacing whole words in templates.

Currently, the Anqi CMS isreplaceThe filter does not have built-in parameters or options at the template level to directly control 'replace whole word only' or support regular expressions. This means that you cannot simply add a parameter (such aswhole_word=true) To change the default substring replacement behavior.

To implement accurate full-word replacement in a template, theoretically it requires writing more complex template logic, such as:

  1. Split a string into an array of words using spaces or punctuation marks (possibly with the helpsplitof a filter).
  2. Traverse these words and determine whether each word exactly matches the target 'old word'.
  3. If a match is found, replace it with "new word".
  4. Finally, reassemble all the words into a complete string (possibly with the help ofjoinof a filter).

However, this method is quite cumbersome to implement in the template syntax of Anqi CMS, and has poor readability, and is usually not recommended to perform such complex logic processing in templates.The main responsibility of the template is to display data, rather than to perform complex string operations.

Utilize the 'Document Keyword Replacement' feature on the Anqi CMS backend to achieve precise replacement

幸运的是,the AnQi CMS fully considers the need for precise content replacement in operation and provides a powerful “Document keyword replacement”function specifically designed to handle this scenario, and it natively supportsregular expressions perfectly solves the problem of “only replacing whole words”.

This function is not for temporary replacement during template rendering, but forthe website content databaseReplace articles in batches and permanently, which is very suitable for operating scenarios that need to ensure the consistency and accuracy of the entire site's content.

How to use the background document keyword replacement feature to achieve full word replacement:

  1. Enter the background feature:Log in to the AnQi CMS backend, navigate to the 'Content Management' menu, and select 'Document Management.'

  2. Find the replacement tool:On the "Document List

  3. Configure the replacement rule:Here, you can set the replacement keyword pairs. The key point is that when you need to replace only whole words, you need to useregular expressions.

    the regular expression in\bIt is a very practical meta-character, which represents "word boundary". It is placed before and after your target word.\bIt can ensure that only when this word appears as an independent whole will it be matched and replaced.

    For example, to replace all independentGowords withGolang, while avoiding influence.GoodThis word, you can configure the replacement rule like this:

    • Old keyword (using regular expression): \bGo\b
    • New keyword: Golang

    The system will recognize\bGo\bAs a regular expression pattern, and only replace those "Go" surrounded by word boundaries, such asGo/Go./Go,/GoBut will not replaceGoodofGo.

  4. Execute replacement:After setting the rules, the system will provide options for previewing or directly executing the replacement. Be sure to check carefully before executing, to confirm that the replacement effect meets expectations.

Summary

When you need to replace content in Anqi CMS, please select the appropriate tool according to your specific needs:

  • For simple substring replacement or just temporary adjustment during front-end page displayCan be used directly in the templatereplaceA filter with ease of use to meet the needs of most lightweight requirements.
  • For those who need accuracy to the whole word, avoid misfire, and hope to make batch, permanent changes to the content in the database.Make sure to use the 'Document Keyword Replacement' feature in the background, combined with regular expressions,\bto achieve powerful and accurate replacement effects.

Understand the difference between these two tools