javascript - 在 jupyter 笔记本中使用匹配替换查找和替换实用程序

标签 javascript regex python-3.x jupyter-notebook regex-group

在我的 jupyter notebook 中,我想替换打印功能 带有包装函数。

我可以将 print 语句与 javascript regexp print\\(.*\\) 匹配,但从那里我不确定如何在替换文本字段中再次使用匹配:

def verb_printer(msg, verb):
    if verb:
        print(msg)

我尝试了类似 verb_printer($1) 的方法来访问匹配项,但没有成功。

我该如何解决这个问题?

最佳答案

您的原始表达式很好,我们只会添加一个捕获组 (),这样当我们用 verb_printer($1) 替换它时,我们想要的字符串已经被捕获:

(print\(.*\))

Demo

测试

const regex = /(print\(.*\))/gm;
const str = `def verb_printer(msg, verb):
    if verb:
        print(msg)
`;
const subst = `verb_printer($1)`;

// The substituted value will be contained in the result variable
const result = str.replace(regex, subst);

console.log(result);

关于javascript - 在 jupyter 笔记本中使用匹配替换查找和替换实用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56427053/

相关文章:

python - 如何在Python 3项目中使用Python 2包?

python - 如何做这份 list

javascript - 在 javascript (moment.js) 中将 UTC 日期转换为本地时间

javascript - 使用不正确的符号更正 JSON 字符串?

php - 检查用户是否存在于数据库(表单)中

regex - 如何在 AWS Lambda 上导入正则表达式

JavaScript 表单提交

python - 在一行中获取捕获组

python - 在辅音之间找到每两个(不重叠的)元音

python-3.x - sqlite 中条件的编号列表