Change Text Using Regular Expressions

To make changes in text content, you need to use regular expressions. The sqf:stringReplace operation allows you to specify a regular expression to find sub-strings of text content and replace them with nodes or other strings. The regular expression need to be specified in the value of the regex attribute.

For example, suppose you have a rule that checks if a link is added as text content and not marked as a link. You can create a quick fix replace the text link with an xref element that has the value of the text link. This will allow you to automatically fix the links from the text and mark them as clickable links.

Example 5. Create a clickable link from a text link

<sqf:fix id="convertToLink">
  <sqf:description>
      <sqf:title>Convert '<sch:value-of select="$linkValue"/>' text link
                 to xref</sqf:title>
  </sqf:description>
  <sqf:stringReplace regex="(http|www)\S+">
      <xref href="{$linkValue}" format="html"/>
  </sqf:stringReplace>
</sqf:fix>

To get the value of the link from the text content, you can use the xsl:analyze-string instructions. This will allow you to process the string content and obtain the content that you need. Then you can make the change in the document using the stringReplace operation.

Example 6. Obtain the value of the link from text

<xsl:variable name="linkValue">
  <xsl:analyze-string select="." regex="(http|www)\S+">
    <xsl:matching-substring>
      <xsl:value-of select="regex-group(0)"/>
    </xsl:matching-substring>
  </xsl:analyze-string>
</xsl:variable>

Therefore, using regular expressions and the sqf:stringReplace operation, you can tag content or you can replace text content with other text content.