I am trying to write a script that will replace every SECOND backtick with a backtick and semi colon. See below for expected behavior:
"`Here is my string`"
Needs to bee:
"`Here is my string`;"
I have found a few helpful answers on stack, such as this one, this one and this one but when I try the replacement on this solution it selects all occurrences, rather than every second occurrence. And on this solution it selects every FIRST occurrence instead of every second one.
As of now I have tried...
str.replace(/\`.*?\`*/g, '`;')
...as well as...
str.replace('\w*\`\b/gm, '`;')
Both have gotten me close but I can't seem to just get every SECOND backtick by itself.
I am trying to write a script that will replace every SECOND backtick with a backtick and semi colon. See below for expected behavior:
"`Here is my string`"
Needs to bee:
"`Here is my string`;"
I have found a few helpful answers on stack, such as this one, this one and this one but when I try the replacement on this solution it selects all occurrences, rather than every second occurrence. And on this solution it selects every FIRST occurrence instead of every second one.
As of now I have tried...
str.replace(/\`.*?\`*/g, '`;')
...as well as...
str.replace('\w*\`\b/gm, '`;')
Both have gotten me close but I can't seem to just get every SECOND backtick by itself.
Share Improve this question edited Aug 10, 2019 at 21:08 Matt Croak asked Aug 10, 2019 at 17:15 Matt CroakMatt Croak 2,8882 gold badges22 silver badges43 bronze badges 4- 3 What are you really trying to achieve here? It looks like you're trying to parse and modify JavaScript code, in which case regex really isn't the most appropriate tool. – jonrsharpe Commented Aug 10, 2019 at 17:17
- 1 This feels like an XY problem, as what @jonrsharpe described. What are you trying to achieve in the end? – Terry Commented Aug 10, 2019 at 17:19
- @jonrsharpe I am experimenting with automatic semi colon insertion and didn't want to insert a semi colon after every single backtick because then you would change the value of the string itself. Can you elaborate on a better tool than regex for this? I am not married to regex it's just what I've been using thus far. – Matt Croak Commented Aug 10, 2019 at 17:29
- Look into AST parsing tools. – jonrsharpe Commented Aug 10, 2019 at 17:32
5 Answers
Reset to default 3If you want to replace every second backtick, you might use a capturing group and a negated character class
In the replacement you could use $1
;`
(`[^`]*)`
Explanation
(
Capture group`[^`]*
Match a backtick, match 0+ times any char except a backtick using a negated character class
)`
Close group 1 and match a backtick
Regex demo
const regex = /(`[^`]*)`/g;
const str = `\`Here is my string\` this is another test \`Here is my string\``;
const result = str.replace(regex, `$1\`;`);
console.log(result);
Finding the second backtick is easy, but finding every second backtick is harder. I think this should work:
"`Here is my string` and `another` and `another`".replace(/`.*?(`.*?`)*?`/g, '$&;');
// -> "`Here is my string`; and `another`; and `another`;"
Let's dig into what that regex means.
- it finds 1st backtick, followed by anything. Note the
?
in.*?
: this makes the match lazy, so that finds the shortest match, not the longest. - it then finds an even number (0, 2, 4) of following backticks (1 + even = odd number of backticks in total), again separated by anything lazily (
.*?
). - it then finds a final backtick
- it replaces that in the string with
$&
(= everything was matched) then adds the semicolon. - The
g
flag at the end then makes it global, so we replace every available match, not just the first one.
Depending on your input you might want to make this more rigorous, it's just a proof of concept. For potentially large inputs especially you may need to watch out for catastrophic backtracking with regular expressions including multiple .*
sections like this.
You can try this
(`[^`]*)`
let str = "`Here is my string` some more string with ``` some more ``` and` and `"
let final = str.replace(/(`[^`]*)`/g,'$1`;')
console.log(final)
I will share the trick i spotted, then was learning regex. To match everything between two specific char, use this construction:
# -- character, that enclosing some content
#[^#]*#
In your case, i believe, you want to use this approach:
`[^`]+(`)
^
use * here, if you want match case, then two backticks do not
contain anything
Here you match every second backtick into first group. After that you can substitute this group to `;
This expression might simply do that:
const regex = /(.*`.*`)/gm;
const str = `\`Here is my string\``;
const subst = `$1;`;
// The substituted value will be contained in the result variable
const result = str.replace(regex, subst);
console.log(result);
The expression is explained on the top right panel of regex101., if you wish to explore/simplify/modify it, and in this link, you can watch how it would match against some sample inputs, if you like.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742362536a4429666.html
评论列表(0条)