Find substring containing the escaped form of a delimiter (Regexp)
Hi All!
I am playing with markdown, dealing with inline markers and escaped characters.
Problem:
I want to transform this: some text *some number * other number* more text
Into this: some text <strong>some number * other number</strong> more text
My current pattern is: /((?!\)*)(.*?)((?!\)*)/g
But the (.*?) group seems to capture the character, so the third group finds the second * character and stops looking for the third one, which should be its target.
Possible solution:
I can solve this problem using negative lookbehind: /((?<!\)*)(.*?)((?<!\)*)/g, but I'd like to avoid it, if it is possible.
Can I modify my other pattern to make it work?
javascript regex
|
show 1 more comment
Hi All!
I am playing with markdown, dealing with inline markers and escaped characters.
Problem:
I want to transform this: some text *some number * other number* more text
Into this: some text <strong>some number * other number</strong> more text
My current pattern is: /((?!\)*)(.*?)((?!\)*)/g
But the (.*?) group seems to capture the character, so the third group finds the second * character and stops looking for the third one, which should be its target.
Possible solution:
I can solve this problem using negative lookbehind: /((?<!\)*)(.*?)((?<!\)*)/g, but I'd like to avoid it, if it is possible.
Can I modify my other pattern to make it work?
javascript regex
2
regex101.com/r/afdKgi/2 ?
– splash58
Jan 2 at 12:21
@splash58 Would you post it as an answer?
– Nekomajin42
Jan 2 at 12:32
/(^|[^\])*(.*?)($|[^\])*/gdoes not work if the*is at the start of the string. Even if you fix that, you won't match\*some number * other number* more textthat should be since the first\defines a backslash.
– Wiktor Stribiżew
Jan 2 at 12:38
1
@WiktorStribiżew regex101.com/r/afdKgi/6
– splash58
Jan 2 at 12:54
@splash58 Still won't work,\*some number * other number* more textstarts with a backslash and an escaped*, but there is a match. This kind of task cannot be solved with.*?and lookarounds.
– Wiktor Stribiżew
Jan 2 at 13:24
|
show 1 more comment
Hi All!
I am playing with markdown, dealing with inline markers and escaped characters.
Problem:
I want to transform this: some text *some number * other number* more text
Into this: some text <strong>some number * other number</strong> more text
My current pattern is: /((?!\)*)(.*?)((?!\)*)/g
But the (.*?) group seems to capture the character, so the third group finds the second * character and stops looking for the third one, which should be its target.
Possible solution:
I can solve this problem using negative lookbehind: /((?<!\)*)(.*?)((?<!\)*)/g, but I'd like to avoid it, if it is possible.
Can I modify my other pattern to make it work?
javascript regex
Hi All!
I am playing with markdown, dealing with inline markers and escaped characters.
Problem:
I want to transform this: some text *some number * other number* more text
Into this: some text <strong>some number * other number</strong> more text
My current pattern is: /((?!\)*)(.*?)((?!\)*)/g
But the (.*?) group seems to capture the character, so the third group finds the second * character and stops looking for the third one, which should be its target.
Possible solution:
I can solve this problem using negative lookbehind: /((?<!\)*)(.*?)((?<!\)*)/g, but I'd like to avoid it, if it is possible.
Can I modify my other pattern to make it work?
javascript regex
javascript regex
asked Jan 2 at 12:11
Nekomajin42Nekomajin42
110139
110139
2
regex101.com/r/afdKgi/2 ?
– splash58
Jan 2 at 12:21
@splash58 Would you post it as an answer?
– Nekomajin42
Jan 2 at 12:32
/(^|[^\])*(.*?)($|[^\])*/gdoes not work if the*is at the start of the string. Even if you fix that, you won't match\*some number * other number* more textthat should be since the first\defines a backslash.
– Wiktor Stribiżew
Jan 2 at 12:38
1
@WiktorStribiżew regex101.com/r/afdKgi/6
– splash58
Jan 2 at 12:54
@splash58 Still won't work,\*some number * other number* more textstarts with a backslash and an escaped*, but there is a match. This kind of task cannot be solved with.*?and lookarounds.
– Wiktor Stribiżew
Jan 2 at 13:24
|
show 1 more comment
2
regex101.com/r/afdKgi/2 ?
– splash58
Jan 2 at 12:21
@splash58 Would you post it as an answer?
– Nekomajin42
Jan 2 at 12:32
/(^|[^\])*(.*?)($|[^\])*/gdoes not work if the*is at the start of the string. Even if you fix that, you won't match\*some number * other number* more textthat should be since the first\defines a backslash.
– Wiktor Stribiżew
Jan 2 at 12:38
1
@WiktorStribiżew regex101.com/r/afdKgi/6
– splash58
Jan 2 at 12:54
@splash58 Still won't work,\*some number * other number* more textstarts with a backslash and an escaped*, but there is a match. This kind of task cannot be solved with.*?and lookarounds.
– Wiktor Stribiżew
Jan 2 at 13:24
2
2
regex101.com/r/afdKgi/2 ?
– splash58
Jan 2 at 12:21
regex101.com/r/afdKgi/2 ?
– splash58
Jan 2 at 12:21
@splash58 Would you post it as an answer?
– Nekomajin42
Jan 2 at 12:32
@splash58 Would you post it as an answer?
– Nekomajin42
Jan 2 at 12:32
/(^|[^\])*(.*?)($|[^\])*/g does not work if the * is at the start of the string. Even if you fix that, you won't match \*some number * other number* more text that should be since the first \ defines a backslash.– Wiktor Stribiżew
Jan 2 at 12:38
/(^|[^\])*(.*?)($|[^\])*/g does not work if the * is at the start of the string. Even if you fix that, you won't match \*some number * other number* more text that should be since the first \ defines a backslash.– Wiktor Stribiżew
Jan 2 at 12:38
1
1
@WiktorStribiżew regex101.com/r/afdKgi/6
– splash58
Jan 2 at 12:54
@WiktorStribiżew regex101.com/r/afdKgi/6
– splash58
Jan 2 at 12:54
@splash58 Still won't work,
\*some number * other number* more text starts with a backslash and an escaped *, but there is a match. This kind of task cannot be solved with .*? and lookarounds.– Wiktor Stribiżew
Jan 2 at 13:24
@splash58 Still won't work,
\*some number * other number* more text starts with a backslash and an escaped *, but there is a match. This kind of task cannot be solved with .*? and lookarounds.– Wiktor Stribiżew
Jan 2 at 13:24
|
show 1 more comment
3 Answers
3
active
oldest
votes
You may use
var str = "some text *some number \* other number* more text";
console.log(
str.replace(/((?:^|[^\])(?:\{2})*)*([^\*]*(?:\[sS][^*\]*)*)*/g,
function($0, $1, $2) { return $1 + '<strong>' + $2.replace(/\([sS])/g, '$1') + '</strong>'; }
)
)The first /((?:^|[^\])(?:\{2})*)*([^\*]*(?:\[sS][^*\]*)*)*/g regex matches all the strings within unescaped *:
((?:^|[^\])(?:\{2})*)- Group 1:
(?:^|[^\])- start of string or a non-backslash
(?:\{2})*- any 0+ occurrences of double backslash (this avoids matching escaped*)
*- a*char
([^\*]*(?:\[sS][^*\]*)*)- Group 2:
[^\*]*- 0+ chars other thanand*
(?:\[sS][^*\]*)*- 0+ sequences of
\[sS]- aand any char
[^*\]*- 0+ chars other thanand*
*- a*char.
The match is passed to the anonymous method as the second argument to the replace method and the contents of Group 2 are processed to "unescape" any escape sequence with .replace(/\([sS])/g, '$1'): \ matches a backslash and ([sS]) matches and captures any char into Group 1, and this is what remains after the replacement with the group placeholder $1.
1
The thing is, I need this stuff for educational purposes, and it's way more complicated than the ideal, but thank you for the detailed explanation.
– Nekomajin42
Jan 2 at 12:34
add a comment |
You can use this
*(.*)*
This uses above regex to find * up to the last *. And than with \(.) i am finding the escaped character and replacing it with captured group.
const regex = /*(.*)*/gm;
const str = `some text *some number \* other number* more text`;
const subst = `<strong>$1</strong>`;
// The substituted value will be contained in the result variable
const result = str.replace(regex, subst);
const finalResult = result.replace(/\(.)/,'$1') //replacing escaped character here
console.log(finalResult);UPDATE: For matching more than one substring
const regex = /*(.*?[^\])*/gm;
const str = `some text *some number \* other number* blah blah *some number \* other number* more text`;
const subst = `<strong>$1</strong>`;
// The substituted value will be contained in the result variable
const result = str.replace(regex, subst);
const finalResult = result.replace(/\(.)/g,'$1') //replacing escaped character here
console.log(finalResult);
Can you modify it to work with multiple instances of the substring withinstr?
– Nekomajin42
Jan 2 at 12:39
@Nekomajin42 check the updated one. let me know if something is missing
– Code Maniac
Jan 2 at 12:58
The updated pattern seems to find only the first and last*marker in the string, and the same markers are ignored between them.
– Nekomajin42
Jan 5 at 14:42
add a comment |
There could be a simpler way to accomplish the same task using the following regex:
\.|*((\.|[^*])+)*
The idea is matching a desired string should occur after all escaped characters are consumed. We try to match all escaped characters using first side of alternation then at the second attempt we want to match our desired pattern if exists.
JS code:
var str = `some text *some number \* other number* more text`
console.log(str.replace(/\.|*((\.|[^*])+)*/g, function(match, $1) {
return $1 ? '<strong>' + $1 + '</strong>' : match;
}));Breakdown:
\.Match an escaped character
|Or
*Match a literal*
(Start of first capturing group
(Start of second capturing group
\.Match an escaped character
|Or
[^*]+Match anything except*
)+End of second capturing group, repeat one or more time
)End of first capturing group
*Match a literal*
This pattern seems to match*substrings outside of the*markers.
– Nekomajin42
Jan 5 at 14:40
It matches but doesn't replace. There is a callback forreplacemethod. Even the answer you accepted has two consecutive calls toreplace()which is complicating things. If you experience any problems please show an example.
– revo
Jan 5 at 18:19
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54006138%2ffind-substring-containing-the-escaped-form-of-a-delimiter-regexp%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
You may use
var str = "some text *some number \* other number* more text";
console.log(
str.replace(/((?:^|[^\])(?:\{2})*)*([^\*]*(?:\[sS][^*\]*)*)*/g,
function($0, $1, $2) { return $1 + '<strong>' + $2.replace(/\([sS])/g, '$1') + '</strong>'; }
)
)The first /((?:^|[^\])(?:\{2})*)*([^\*]*(?:\[sS][^*\]*)*)*/g regex matches all the strings within unescaped *:
((?:^|[^\])(?:\{2})*)- Group 1:
(?:^|[^\])- start of string or a non-backslash
(?:\{2})*- any 0+ occurrences of double backslash (this avoids matching escaped*)
*- a*char
([^\*]*(?:\[sS][^*\]*)*)- Group 2:
[^\*]*- 0+ chars other thanand*
(?:\[sS][^*\]*)*- 0+ sequences of
\[sS]- aand any char
[^*\]*- 0+ chars other thanand*
*- a*char.
The match is passed to the anonymous method as the second argument to the replace method and the contents of Group 2 are processed to "unescape" any escape sequence with .replace(/\([sS])/g, '$1'): \ matches a backslash and ([sS]) matches and captures any char into Group 1, and this is what remains after the replacement with the group placeholder $1.
1
The thing is, I need this stuff for educational purposes, and it's way more complicated than the ideal, but thank you for the detailed explanation.
– Nekomajin42
Jan 2 at 12:34
add a comment |
You may use
var str = "some text *some number \* other number* more text";
console.log(
str.replace(/((?:^|[^\])(?:\{2})*)*([^\*]*(?:\[sS][^*\]*)*)*/g,
function($0, $1, $2) { return $1 + '<strong>' + $2.replace(/\([sS])/g, '$1') + '</strong>'; }
)
)The first /((?:^|[^\])(?:\{2})*)*([^\*]*(?:\[sS][^*\]*)*)*/g regex matches all the strings within unescaped *:
((?:^|[^\])(?:\{2})*)- Group 1:
(?:^|[^\])- start of string or a non-backslash
(?:\{2})*- any 0+ occurrences of double backslash (this avoids matching escaped*)
*- a*char
([^\*]*(?:\[sS][^*\]*)*)- Group 2:
[^\*]*- 0+ chars other thanand*
(?:\[sS][^*\]*)*- 0+ sequences of
\[sS]- aand any char
[^*\]*- 0+ chars other thanand*
*- a*char.
The match is passed to the anonymous method as the second argument to the replace method and the contents of Group 2 are processed to "unescape" any escape sequence with .replace(/\([sS])/g, '$1'): \ matches a backslash and ([sS]) matches and captures any char into Group 1, and this is what remains after the replacement with the group placeholder $1.
1
The thing is, I need this stuff for educational purposes, and it's way more complicated than the ideal, but thank you for the detailed explanation.
– Nekomajin42
Jan 2 at 12:34
add a comment |
You may use
var str = "some text *some number \* other number* more text";
console.log(
str.replace(/((?:^|[^\])(?:\{2})*)*([^\*]*(?:\[sS][^*\]*)*)*/g,
function($0, $1, $2) { return $1 + '<strong>' + $2.replace(/\([sS])/g, '$1') + '</strong>'; }
)
)The first /((?:^|[^\])(?:\{2})*)*([^\*]*(?:\[sS][^*\]*)*)*/g regex matches all the strings within unescaped *:
((?:^|[^\])(?:\{2})*)- Group 1:
(?:^|[^\])- start of string or a non-backslash
(?:\{2})*- any 0+ occurrences of double backslash (this avoids matching escaped*)
*- a*char
([^\*]*(?:\[sS][^*\]*)*)- Group 2:
[^\*]*- 0+ chars other thanand*
(?:\[sS][^*\]*)*- 0+ sequences of
\[sS]- aand any char
[^*\]*- 0+ chars other thanand*
*- a*char.
The match is passed to the anonymous method as the second argument to the replace method and the contents of Group 2 are processed to "unescape" any escape sequence with .replace(/\([sS])/g, '$1'): \ matches a backslash and ([sS]) matches and captures any char into Group 1, and this is what remains after the replacement with the group placeholder $1.
You may use
var str = "some text *some number \* other number* more text";
console.log(
str.replace(/((?:^|[^\])(?:\{2})*)*([^\*]*(?:\[sS][^*\]*)*)*/g,
function($0, $1, $2) { return $1 + '<strong>' + $2.replace(/\([sS])/g, '$1') + '</strong>'; }
)
)The first /((?:^|[^\])(?:\{2})*)*([^\*]*(?:\[sS][^*\]*)*)*/g regex matches all the strings within unescaped *:
((?:^|[^\])(?:\{2})*)- Group 1:
(?:^|[^\])- start of string or a non-backslash
(?:\{2})*- any 0+ occurrences of double backslash (this avoids matching escaped*)
*- a*char
([^\*]*(?:\[sS][^*\]*)*)- Group 2:
[^\*]*- 0+ chars other thanand*
(?:\[sS][^*\]*)*- 0+ sequences of
\[sS]- aand any char
[^*\]*- 0+ chars other thanand*
*- a*char.
The match is passed to the anonymous method as the second argument to the replace method and the contents of Group 2 are processed to "unescape" any escape sequence with .replace(/\([sS])/g, '$1'): \ matches a backslash and ([sS]) matches and captures any char into Group 1, and this is what remains after the replacement with the group placeholder $1.
var str = "some text *some number \* other number* more text";
console.log(
str.replace(/((?:^|[^\])(?:\{2})*)*([^\*]*(?:\[sS][^*\]*)*)*/g,
function($0, $1, $2) { return $1 + '<strong>' + $2.replace(/\([sS])/g, '$1') + '</strong>'; }
)
)var str = "some text *some number \* other number* more text";
console.log(
str.replace(/((?:^|[^\])(?:\{2})*)*([^\*]*(?:\[sS][^*\]*)*)*/g,
function($0, $1, $2) { return $1 + '<strong>' + $2.replace(/\([sS])/g, '$1') + '</strong>'; }
)
)answered Jan 2 at 12:21
Wiktor StribiżewWiktor Stribiżew
322k16143224
322k16143224
1
The thing is, I need this stuff for educational purposes, and it's way more complicated than the ideal, but thank you for the detailed explanation.
– Nekomajin42
Jan 2 at 12:34
add a comment |
1
The thing is, I need this stuff for educational purposes, and it's way more complicated than the ideal, but thank you for the detailed explanation.
– Nekomajin42
Jan 2 at 12:34
1
1
The thing is, I need this stuff for educational purposes, and it's way more complicated than the ideal, but thank you for the detailed explanation.
– Nekomajin42
Jan 2 at 12:34
The thing is, I need this stuff for educational purposes, and it's way more complicated than the ideal, but thank you for the detailed explanation.
– Nekomajin42
Jan 2 at 12:34
add a comment |
You can use this
*(.*)*
This uses above regex to find * up to the last *. And than with \(.) i am finding the escaped character and replacing it with captured group.
const regex = /*(.*)*/gm;
const str = `some text *some number \* other number* more text`;
const subst = `<strong>$1</strong>`;
// The substituted value will be contained in the result variable
const result = str.replace(regex, subst);
const finalResult = result.replace(/\(.)/,'$1') //replacing escaped character here
console.log(finalResult);UPDATE: For matching more than one substring
const regex = /*(.*?[^\])*/gm;
const str = `some text *some number \* other number* blah blah *some number \* other number* more text`;
const subst = `<strong>$1</strong>`;
// The substituted value will be contained in the result variable
const result = str.replace(regex, subst);
const finalResult = result.replace(/\(.)/g,'$1') //replacing escaped character here
console.log(finalResult);
Can you modify it to work with multiple instances of the substring withinstr?
– Nekomajin42
Jan 2 at 12:39
@Nekomajin42 check the updated one. let me know if something is missing
– Code Maniac
Jan 2 at 12:58
The updated pattern seems to find only the first and last*marker in the string, and the same markers are ignored between them.
– Nekomajin42
Jan 5 at 14:42
add a comment |
You can use this
*(.*)*
This uses above regex to find * up to the last *. And than with \(.) i am finding the escaped character and replacing it with captured group.
const regex = /*(.*)*/gm;
const str = `some text *some number \* other number* more text`;
const subst = `<strong>$1</strong>`;
// The substituted value will be contained in the result variable
const result = str.replace(regex, subst);
const finalResult = result.replace(/\(.)/,'$1') //replacing escaped character here
console.log(finalResult);UPDATE: For matching more than one substring
const regex = /*(.*?[^\])*/gm;
const str = `some text *some number \* other number* blah blah *some number \* other number* more text`;
const subst = `<strong>$1</strong>`;
// The substituted value will be contained in the result variable
const result = str.replace(regex, subst);
const finalResult = result.replace(/\(.)/g,'$1') //replacing escaped character here
console.log(finalResult);
Can you modify it to work with multiple instances of the substring withinstr?
– Nekomajin42
Jan 2 at 12:39
@Nekomajin42 check the updated one. let me know if something is missing
– Code Maniac
Jan 2 at 12:58
The updated pattern seems to find only the first and last*marker in the string, and the same markers are ignored between them.
– Nekomajin42
Jan 5 at 14:42
add a comment |
You can use this
*(.*)*
This uses above regex to find * up to the last *. And than with \(.) i am finding the escaped character and replacing it with captured group.
const regex = /*(.*)*/gm;
const str = `some text *some number \* other number* more text`;
const subst = `<strong>$1</strong>`;
// The substituted value will be contained in the result variable
const result = str.replace(regex, subst);
const finalResult = result.replace(/\(.)/,'$1') //replacing escaped character here
console.log(finalResult);UPDATE: For matching more than one substring
const regex = /*(.*?[^\])*/gm;
const str = `some text *some number \* other number* blah blah *some number \* other number* more text`;
const subst = `<strong>$1</strong>`;
// The substituted value will be contained in the result variable
const result = str.replace(regex, subst);
const finalResult = result.replace(/\(.)/g,'$1') //replacing escaped character here
console.log(finalResult);You can use this
*(.*)*
This uses above regex to find * up to the last *. And than with \(.) i am finding the escaped character and replacing it with captured group.
const regex = /*(.*)*/gm;
const str = `some text *some number \* other number* more text`;
const subst = `<strong>$1</strong>`;
// The substituted value will be contained in the result variable
const result = str.replace(regex, subst);
const finalResult = result.replace(/\(.)/,'$1') //replacing escaped character here
console.log(finalResult);UPDATE: For matching more than one substring
const regex = /*(.*?[^\])*/gm;
const str = `some text *some number \* other number* blah blah *some number \* other number* more text`;
const subst = `<strong>$1</strong>`;
// The substituted value will be contained in the result variable
const result = str.replace(regex, subst);
const finalResult = result.replace(/\(.)/g,'$1') //replacing escaped character here
console.log(finalResult);const regex = /*(.*)*/gm;
const str = `some text *some number \* other number* more text`;
const subst = `<strong>$1</strong>`;
// The substituted value will be contained in the result variable
const result = str.replace(regex, subst);
const finalResult = result.replace(/\(.)/,'$1') //replacing escaped character here
console.log(finalResult);const regex = /*(.*)*/gm;
const str = `some text *some number \* other number* more text`;
const subst = `<strong>$1</strong>`;
// The substituted value will be contained in the result variable
const result = str.replace(regex, subst);
const finalResult = result.replace(/\(.)/,'$1') //replacing escaped character here
console.log(finalResult);const regex = /*(.*?[^\])*/gm;
const str = `some text *some number \* other number* blah blah *some number \* other number* more text`;
const subst = `<strong>$1</strong>`;
// The substituted value will be contained in the result variable
const result = str.replace(regex, subst);
const finalResult = result.replace(/\(.)/g,'$1') //replacing escaped character here
console.log(finalResult);const regex = /*(.*?[^\])*/gm;
const str = `some text *some number \* other number* blah blah *some number \* other number* more text`;
const subst = `<strong>$1</strong>`;
// The substituted value will be contained in the result variable
const result = str.replace(regex, subst);
const finalResult = result.replace(/\(.)/g,'$1') //replacing escaped character here
console.log(finalResult);edited Jan 2 at 12:57
answered Jan 2 at 12:19
Code ManiacCode Maniac
8,9202630
8,9202630
Can you modify it to work with multiple instances of the substring withinstr?
– Nekomajin42
Jan 2 at 12:39
@Nekomajin42 check the updated one. let me know if something is missing
– Code Maniac
Jan 2 at 12:58
The updated pattern seems to find only the first and last*marker in the string, and the same markers are ignored between them.
– Nekomajin42
Jan 5 at 14:42
add a comment |
Can you modify it to work with multiple instances of the substring withinstr?
– Nekomajin42
Jan 2 at 12:39
@Nekomajin42 check the updated one. let me know if something is missing
– Code Maniac
Jan 2 at 12:58
The updated pattern seems to find only the first and last*marker in the string, and the same markers are ignored between them.
– Nekomajin42
Jan 5 at 14:42
Can you modify it to work with multiple instances of the substring within
str?– Nekomajin42
Jan 2 at 12:39
Can you modify it to work with multiple instances of the substring within
str?– Nekomajin42
Jan 2 at 12:39
@Nekomajin42 check the updated one. let me know if something is missing
– Code Maniac
Jan 2 at 12:58
@Nekomajin42 check the updated one. let me know if something is missing
– Code Maniac
Jan 2 at 12:58
The updated pattern seems to find only the first and last
* marker in the string, and the same markers are ignored between them.– Nekomajin42
Jan 5 at 14:42
The updated pattern seems to find only the first and last
* marker in the string, and the same markers are ignored between them.– Nekomajin42
Jan 5 at 14:42
add a comment |
There could be a simpler way to accomplish the same task using the following regex:
\.|*((\.|[^*])+)*
The idea is matching a desired string should occur after all escaped characters are consumed. We try to match all escaped characters using first side of alternation then at the second attempt we want to match our desired pattern if exists.
JS code:
var str = `some text *some number \* other number* more text`
console.log(str.replace(/\.|*((\.|[^*])+)*/g, function(match, $1) {
return $1 ? '<strong>' + $1 + '</strong>' : match;
}));Breakdown:
\.Match an escaped character
|Or
*Match a literal*
(Start of first capturing group
(Start of second capturing group
\.Match an escaped character
|Or
[^*]+Match anything except*
)+End of second capturing group, repeat one or more time
)End of first capturing group
*Match a literal*
This pattern seems to match*substrings outside of the*markers.
– Nekomajin42
Jan 5 at 14:40
It matches but doesn't replace. There is a callback forreplacemethod. Even the answer you accepted has two consecutive calls toreplace()which is complicating things. If you experience any problems please show an example.
– revo
Jan 5 at 18:19
add a comment |
There could be a simpler way to accomplish the same task using the following regex:
\.|*((\.|[^*])+)*
The idea is matching a desired string should occur after all escaped characters are consumed. We try to match all escaped characters using first side of alternation then at the second attempt we want to match our desired pattern if exists.
JS code:
var str = `some text *some number \* other number* more text`
console.log(str.replace(/\.|*((\.|[^*])+)*/g, function(match, $1) {
return $1 ? '<strong>' + $1 + '</strong>' : match;
}));Breakdown:
\.Match an escaped character
|Or
*Match a literal*
(Start of first capturing group
(Start of second capturing group
\.Match an escaped character
|Or
[^*]+Match anything except*
)+End of second capturing group, repeat one or more time
)End of first capturing group
*Match a literal*
This pattern seems to match*substrings outside of the*markers.
– Nekomajin42
Jan 5 at 14:40
It matches but doesn't replace. There is a callback forreplacemethod. Even the answer you accepted has two consecutive calls toreplace()which is complicating things. If you experience any problems please show an example.
– revo
Jan 5 at 18:19
add a comment |
There could be a simpler way to accomplish the same task using the following regex:
\.|*((\.|[^*])+)*
The idea is matching a desired string should occur after all escaped characters are consumed. We try to match all escaped characters using first side of alternation then at the second attempt we want to match our desired pattern if exists.
JS code:
var str = `some text *some number \* other number* more text`
console.log(str.replace(/\.|*((\.|[^*])+)*/g, function(match, $1) {
return $1 ? '<strong>' + $1 + '</strong>' : match;
}));Breakdown:
\.Match an escaped character
|Or
*Match a literal*
(Start of first capturing group
(Start of second capturing group
\.Match an escaped character
|Or
[^*]+Match anything except*
)+End of second capturing group, repeat one or more time
)End of first capturing group
*Match a literal*
There could be a simpler way to accomplish the same task using the following regex:
\.|*((\.|[^*])+)*
The idea is matching a desired string should occur after all escaped characters are consumed. We try to match all escaped characters using first side of alternation then at the second attempt we want to match our desired pattern if exists.
JS code:
var str = `some text *some number \* other number* more text`
console.log(str.replace(/\.|*((\.|[^*])+)*/g, function(match, $1) {
return $1 ? '<strong>' + $1 + '</strong>' : match;
}));Breakdown:
\.Match an escaped character
|Or
*Match a literal*
(Start of first capturing group
(Start of second capturing group
\.Match an escaped character
|Or
[^*]+Match anything except*
)+End of second capturing group, repeat one or more time
)End of first capturing group
*Match a literal*
var str = `some text *some number \* other number* more text`
console.log(str.replace(/\.|*((\.|[^*])+)*/g, function(match, $1) {
return $1 ? '<strong>' + $1 + '</strong>' : match;
}));var str = `some text *some number \* other number* more text`
console.log(str.replace(/\.|*((\.|[^*])+)*/g, function(match, $1) {
return $1 ? '<strong>' + $1 + '</strong>' : match;
}));answered Jan 2 at 13:08
revorevo
33.6k135086
33.6k135086
This pattern seems to match*substrings outside of the*markers.
– Nekomajin42
Jan 5 at 14:40
It matches but doesn't replace. There is a callback forreplacemethod. Even the answer you accepted has two consecutive calls toreplace()which is complicating things. If you experience any problems please show an example.
– revo
Jan 5 at 18:19
add a comment |
This pattern seems to match*substrings outside of the*markers.
– Nekomajin42
Jan 5 at 14:40
It matches but doesn't replace. There is a callback forreplacemethod. Even the answer you accepted has two consecutive calls toreplace()which is complicating things. If you experience any problems please show an example.
– revo
Jan 5 at 18:19
This pattern seems to match
* substrings outside of the * markers.– Nekomajin42
Jan 5 at 14:40
This pattern seems to match
* substrings outside of the * markers.– Nekomajin42
Jan 5 at 14:40
It matches but doesn't replace. There is a callback for
replace method. Even the answer you accepted has two consecutive calls to replace() which is complicating things. If you experience any problems please show an example.– revo
Jan 5 at 18:19
It matches but doesn't replace. There is a callback for
replace method. Even the answer you accepted has two consecutive calls to replace() which is complicating things. If you experience any problems please show an example.– revo
Jan 5 at 18:19
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54006138%2ffind-substring-containing-the-escaped-form-of-a-delimiter-regexp%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
regex101.com/r/afdKgi/2 ?
– splash58
Jan 2 at 12:21
@splash58 Would you post it as an answer?
– Nekomajin42
Jan 2 at 12:32
/(^|[^\])*(.*?)($|[^\])*/gdoes not work if the*is at the start of the string. Even if you fix that, you won't match\*some number * other number* more textthat should be since the first\defines a backslash.– Wiktor Stribiżew
Jan 2 at 12:38
1
@WiktorStribiżew regex101.com/r/afdKgi/6
– splash58
Jan 2 at 12:54
@splash58 Still won't work,
\*some number * other number* more textstarts with a backslash and an escaped*, but there is a match. This kind of task cannot be solved with.*?and lookarounds.– Wiktor Stribiżew
Jan 2 at 13:24