Can/how do you do a multipart regex replace that looks for content within some tags then moves any space padding outside of the tags?
sample start string:
random text to ignore** some text **other text to ignore** other important text** blah blah blah
result:
random text to ignore **some text** other text to ignore **other important text** blah blah blah
basically trying to help a markdown to html library parse correctly where it is having trouble with spaces right after MD age so i want to do a quick scrub first. The conversion will be done in python with markdown2 or in javascript with showdown.js.
I am hoping for a syntax like s.replace(regex, '$1**$2**$3') where 1 and 3 would be spaces within the tags, if they exist
You can use
text.replace(/\*{2}(\s*)(.*?)(\s*)\*{2}/g, '$1**$2**$3')
See the regex demo. Details:
\*{2} - a ** string(\s*) - Group 1 ($1): zero or more whitespaces(.*?) - Group 2: any zero or more chars other than line break chars as few as possible (use [^]*? or [\w\W]*? to match across line breaks)(\s*) - Group 3 ($3): zero or more whitespaces\*{2} - a ** stringSee a JavaScript demo:
const text = "random text to ignore** some text **other text to ignore** other important text** blah blah blah";
console.log(text.replace(/\*{2}(\s*)(.*?)(\s*)\*{2}/g, '$1**$2**$3'));