I'm trying to match strings that are surrounded by backtick's but they aren't surrounded by triple backtick's.
For example:
match: `I should be matched`
should not match: ``` `Hello world` ```
This is what I've tried so far: https://regex101.com/r/P4MhiM/1
CodePudding user response:
i get i pass this test case
first capture all match in single quote
\`(?:. )\`
second filter the matchs only char not backticks and white space
\`(?:[^\`||\s] )\`
https://regex101.com/r/oVE7zi/1
CodePudding user response:
Match and capture a part of the match, then use custom logic in the replacement:
const text = "\n``` `Hello world` ```\n\n\n`Matched!`\n";
const rx = /```.*?```|`([^`\n] )`/g;
console.log(
text.replace(rx, (x, y) => y ? `<code>${y}</code>` : x)
)
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>
See the regex demo. Details:
```.*?```
- strings between triple backticks (with no line breaks in between backticks)|
- or`([^`\n] )`
- strings between backticks without line break chars.