# coding=utf8
# the above tag defines encoding for this document and is for Python 2.x compatibility
import re
regex = r"[`\\]|\${"
test_str = ("// source https://stackoverflow.com/a/75688937/6404439\n"
"import React from 'react';\n"
"import SyntaxHighlighter from 'react-syntax-highlighter';\n"
"import { a11yDark } from 'react-syntax-highlighter/dist/esm/styles/hljs';\n\n"
"const CodeBlock: React.FC<{ codestring: string }> = ({ codestring }) => {\n"
" return (\n"
" <SyntaxHighlighter\n"
" language=\"javascript\"\n"
" style={a11yDark}\n"
" showLineNumbers>\n"
" {codestring}\n"
" </SyntaxHighlighter>\n"
" );\n"
"};\n\n"
"function App() {\n\n"
" var code = `function $initHighlight(block, cls) {\n"
" try {\n"
" if (cls.search(/\\bno\\-highlight\\b/) != -1)\n"
" return process(block, true, 0x0F) +\n"
" \\` class=\"\\${cls}\"\\`;\n"
" } catch (e) {\n"
" /* handle exception */\n"
" }\n"
" for (var i = 0 / 2; i < classes.length; i++) {\n"
" if (checkCondition(classes[i]) === undefined)\n"
" return /\\d+[\\s/]/g;\n"
" }\n"
" return 0xA;\n"
" }\n\n"
" export $initHighlight;`;\n\n"
" return (\n"
" <div className=\"w-1/2 bg-blue-700\">\n"
" <CodeBlock codestring={code} />\n"
" </div>\n"
" );\n"
"}\n\n"
"export default App;")
subst = "\\\\$&"
# You can manually specify the number of replacements by changing the 4th argument
result = re.sub(regex, subst, test_str, 0, re.MULTILINE)
if result:
print (result)
# Note: for Python 2.7 compatibility, use ur"" to prefix the regex and u"" to prefix the test string and substitution.
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for Python, please visit: https://docs.python.org/3/library/re.html