正则表达式notepad ++插入,不能替换

问题描述

在正则表达式(notepad ++)中,我要搜索( )|(:)|(_)|(\.),并在行和“。”下的空白,冒号之前插入\

搜索示例:abcd:1234 jiod.8ufd_adfd

结果:abcd\:1234\ jiod\.8ufd\_adfd

简而言之,我如何引用替换表达式中找到的内容

请注意,在示例中它不是\1,\2,\3 or \4,因为我需要包含找到的内容,所以无法知道找到的内容,是吗?

解决方法

您可以使用单个字符类(而不是与捕获组一起使用替代字符)来匹配列出的字符之一

在替换中,使用$&来引用匹配的文本并在反斜杠前加上反斜杠。

匹配

[:\h._]

替换为

\\$&

字符类与冒号,水平空白字符,点或下划线匹配。

Regex demo

enter image description here

,

没有诸如insert之类的东西,因为如果您考虑一下,insert就是将原件替换为也包含旧文本的新字符串。

相反,请尝试以下操作:搜索var ss = SpreadsheetApp.getActiveSpreadsheet(); var sheet = ss.getSheetByName('Sheet1'); function lockRanges() { var row = 2; var col = 1; // Get last row with data in sheet var lastbutBeforeRow = sheet.getLastRow()-1; var lastRow = sheet.getLastRow(); //Loop until last row in sheet is passed while(row <= lastbutBeforeRow){ //Lock current cell lockRange(row,col); row = row + 1; } } function lockRange(row,col){ var range = sheet.getRange(row,col) var protection = range.protect().setDescription('Protected,row ' + row); var me = Session.getEffectiveUser(); protection.addEditor(me); protection.removeEditors(protection.getEditors()); if (protection.canDomainEdit()) { protection.setDomainEdit(false); } } (您的原始正则表达式毫无意义)并替换为([ :_.])(即斜杠后跟原始文本)。