【问题标题】:Creating a regular expression that replaces strings in quotes with empty strings创建一个正则表达式,用空字符串替换引号中的字符串
【发布时间】:2022-07-18 21:44:12
【问题描述】:

如何创建一个正则表达式来查找包含在 """''' 中的文本,例如:

hello """
long text here,
e.g. a private SSH key
"""
this is a name for testing

'''
this is another multi-line 
stuff.
'''

我想得到如下输出:

hello
this is a name for testing

"""''' 中的所有文本替换为空字符串。

【问题讨论】:

  • 你尝试过什么,它到底有什么问题?

标签: javascript node.js regex


【解决方案1】:

我们可以尝试针对所有多行引用的术语进行正则表达式替换。我们将使用全点模式来确保匹配出现在换行符之间。

var input = `
hello """
long text here,
e.g. a private SSH key
"""
this is a name for testing

'''
this is another multi-line 
stuff.
'''`;
var output = input.replace(/""".*?"""|'''.*?'''/sg, '').trim();
console.log(output);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-26
    • 2021-03-16
    • 1970-01-01
    • 2023-03-21
    • 1970-01-01
    • 2017-02-04
    相关资源
    最近更新 更多