简体   繁体   English

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

[英]PhpStorm: how can I replace specific word with regex

I have this script in my php page : 我的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;"> 

In PhpStorm I Ctrl + Shift + R to replace all REGEX to <?php echo __("REGEX") ?> , but not the lines containing the __ function 在PhpStorm中,我按Ctrl + Shift + R将所有REGEX替换为<?php echo __("REGEX") ?> ,但不替换包含__函数的行

I've used the "(?m)^(?!.*\\__\\b).*$" expression to select all lines that don't contain __ function and I use .*add-new-head.* expression to select the attribute to help me change the value. 我已经使用"(?m)^(?!.*\\__\\b).*$"表达式来选择所有不包含__函数的行,而我使用了.*add-new-head.*表达式选择属性以帮助我更改值。

My question is how I can combine the first and the second expression exactly to replace all REGEX with <?php echo __("REGEX") ?> ? 我的问题是我如何才能准确地组合第一和第二个表达式,以用<?php echo __("REGEX") ?>替换所有REGEX

This Should Work. 这应该工作。

Tested on Notepad++ 在Notepad ++上测试

Search By: 搜索依据:

REGEX$

Replace with: 用。。。来代替:

__("REGEX") __(“ REGEX”)

Output: 输出:

<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;"> 

Find (REGEX)$ assuming REGEX is always at the end of line with no characters after it. 假设REGEX始终位于行尾且后面没有字符,则查找(REGEX)$

Then replace with <?php echo __("\\1") ?> where \\1 corresponds to the first capture group. 然后替换为<?php echo __("\\1") ?> ,其中\\1对应于第一个捕获组。

UPDATE: 更新:

Since you want to capture everything between "add-new-head"> and the next tag start < you can use the following. 既然你想捕捉之间的一切"add-new-head">和下一个标签开始<你可以使用下面的。

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

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

Output: 输出:

<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;"> 

you can find the demo here 你可以在这里找到演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM