How can I create a regular expression that finds the text wrapped in """ or ''' for example:
hello """ long text here, e.g. a private SSH key """ this is a name for testing ''' this is another multi-line stuff. '''
I want to get the output like:
hello this is a name for testing
With all the text that are in """ or ''' replaced with an empty string.
Advertisement
Answer
Use """|''' as delimiters (with a 1 back-reference for the closing one), a non-greedy match-all (.*?) and the s (dot-all) and g (global) flags.
Remove leading and trailing white-space with trim().
const str = `hello """
long text here,
e.g. a private SSH key
"""
this is a name for testing
'''
this is another multi-line
stuff.
'''`
console.log(str.replace(/("""|''').*?1/gs, "").trim());