PhpStorm:如何用正则表达式替换特定单词

时间:2017-02-22 18:58:12

标签: regex phpstorm

我的php页面中有这个脚本:

<div id="add-new-head"><?php echo __("EDIT_MY_ACCOUNT_DETAILS") ?>
<a href="javascript:{}" title="Close" id="closeBox" onClick="document.getElementById('add-new').style.display='none';"><div class="close">X</div></a></div>
 <div class="inside">    
    <div id="mess" style="position: relative; top: 0;">     
....    ....    ....
    <div id="add-new-head"><?php echo __("EDIT_MY_ACCOUNT_DETAILS") ?>
<a href="javascript:{}" title="Close" id="closeBox" onClick="document.getElementById('add-new').style.display='none';"><div class="close">X</div></a></div>
 <div class="inside">    
    <div id="mess" style="position: relative; top: 0;">     

        ....    ....    ....
    <div id="add-new-head">REGEX
<a href="javascript:{}" title="Close" id="closeBox" onClick="document.getElementById('add-new').style.display='none';"><div class="close">X</div></a></div>
 <div class="inside">    
    <div id="mess" style="position: relative; top: 0;">     

                ....    ....    ....
    <div id="add-new-head"><?php echo __("REGEX") ?>
<a href="javascript:{}" title="Close" id="closeBox" onClick="document.getElementById('add-new').style.display='none';"><div class="close">X</div></a></div>
 <div class="inside">    
    <div id="mess" style="position: relative; top: 0;">     

                ....    ....    ....
    <div id="add-new-head">REGEX
<a href="javascript:{}" title="Close" id="closeBox" onClick="document.getElementById('add-new').style.display='none';"><div class="close">X</div></a></div>
 <div class="inside">    
    <div id="mess" style="position: relative; top: 0;"> 

在PhpStorm我 Ctrl + Shift + R 将所有REGEX替换为<?php echo __("REGEX") ?>,但不包含包含__函数的行

我已使用"(?m)^(?!.*\__\b).*$"表达式选择不包含__函数的所有行,并使用.*add-new-head.*表达式选择属性以帮助我更改价值。

我的问题是如何将第一个和第二个表达式完全结合起来用REGEX替换所有<?php echo __("REGEX") ?>

2 个答案:

答案 0 :(得分:2)

这应该有效。

在Notepad ++上测试

搜索者:

REGEX$

替换为:

__(&#34; REGEX&#34;)

输出:

<div id="add-new-head"><?php echo __("EDIT_MY_ACCOUNT_DETAILS") ?>
<a href="javascript:{}" title="Close" id="closeBox" onClick="document.getElementById('add-new').style.display='none';"><div class="close">X</div></a></div>
 <div class="inside">    
    <div id="mess" style="position: relative; top: 0;">     
....    ....    ....
    <div id="add-new-head"><?php echo __("EDIT_MY_ACCOUNT_DETAILS") ?>
<a href="javascript:{}" title="Close" id="closeBox" onClick="document.getElementById('add-new').style.display='none';"><div class="close">X</div></a></div>
 <div class="inside">    
    <div id="mess" style="position: relative; top: 0;">     

        ....    ....    ....
    <div id="add-new-head"> __"REGEX" 
<a href="javascript:{}" title="Close" id="closeBox" onClick="document.getElementById('add-new').style.display='none';"><div class="close">X</div></a></div>
 <div class="inside">    
    <div id="mess" style="position: relative; top: 0;">     

                ....    ....    ....
    <div id="add-new-head"><?php echo __("REGEX") ?>
<a href="javascript:{}" title="Close" id="closeBox" onClick="document.getElementById('add-new').style.display='none';"><div class="close">X</div></a></div>
 <div class="inside">    
    <div id="mess" style="position: relative; top: 0;">     

                ....    ....    ....
    <div id="add-new-head"> __"REGEX" 
<a href="javascript:{}" title="Close" id="closeBox" onClick="document.getElementById('add-new').style.display='none';"><div class="close">X</div></a></div>
 <div class="inside">    
    <div id="mess" style="position: relative; top: 0;"> 

答案 1 :(得分:1)

查找(REGEX)$,假设REGEX始终位于行尾,后面没有字符。

然后替换为<?php echo __("\1") ?>,其中\1对应于第一个捕获组。

<强>更新

由于您希望捕获"add-new-head">与下一个标记start <之间的所有内容,因此您可以使用以下内容。

查找:("add-new-head">)([^<\s]+)

替换为:\1<?php echo __("\2") ?>

输出:

<div id="add-new-head"><?php echo __("EDIT_MY_ACCOUNT_DETAILS") ?>
<a href="javascript:{}" title="Close" id="closeBox" onClick="document.getElementById('add-new').style.display='none';"><div class="close">X</div></a></div>
 <div class="inside">    
    <div id="mess" style="position: relative; top: 0;">     
....    ....    ....
    <div id="add-new-head"><?php echo __("EDIT_MY_ACCOUNT_DETAILS") ?>
<a href="javascript:{}" title="Close" id="closeBox" onClick="document.getElementById('add-new').style.display='none';"><div class="close">X</div></a></div>
 <div class="inside">    
    <div id="mess" style="position: relative; top: 0;">     

        ....    ....    ....
    <div id="add-new-head"><?php echo __("REGEX") ?>
<a href="javascript:{}" title="Close" id="closeBox" onClick="document.getElementById('add-new').style.display='none';"><div class="close">X</div></a></div>
 <div class="inside">    
    <div id="mess" style="position: relative; top: 0;">     

                ....    ....    ....
    <div id="add-new-head"><?php echo __("REGEX") ?>
<a href="javascript:{}" title="Close" id="closeBox" onClick="document.getElementById('add-new').style.display='none';"><div class="close">X</div></a></div>
 <div class="inside">    
    <div id="mess" style="position: relative; top: 0;">     

                ....    ....    ....
    <div id="add-new-head"><?php echo __("REGEX") ?>
<a href="javascript:{}" title="Close" id="closeBox" onClick="document.getElementById('add-new').style.display='none';"><div class="close">X</div></a></div>
 <div class="inside">    
    <div id="mess" style="position: relative; top: 0;"> 

您可以找到演示here