我怎样才能做一个从数组调用的正则表达式替换
How can I do a regex replacement called from an array
我正在尝试对字符串执行一些替换操作。
替换操作存储在一个数组中。
一切顺利,直到第 3 个动作。
我无法让那个工作,也不明白为什么。
它不会给出错误,但不会执行最后一次替换
"/(\d{1,5})/([A-Z]{3})/g","/"
第一个替换用斜杠替换所有空格
第二个替换向下移动右侧的字符串(插入新行)
第 3 次替换应将 ESB
字符串与其前面的 4 位数字交换
例如
AR/PAG30079XZ/2213/ESB/ABC
会变成
AR/PAG30079XZ/ESB/2213/ABC
这是最后一部分不起作用
这是我目前得到的:
function autoPlay(){
var arr = [
["/ +/g","/"],
["/(?=[A-Z]R/)/g","$&\n"],
["/(\d{1,5})/([A-Z]{3})/g","/"]
];
var arrL = arr.length;
var str = document.getElementById('val');
for (var i = 0; i < arrL; i++) {
var parts = /\/(.*)\/(.*)/.exec(arr[i][0]);
var restoredRegex = new RegExp(parts[1], parts[2]);
var result = str.innerHTML.replace(restoredRegex, arr[i][1]);
str.innerHTML = result
}
}
#val{
border: 1px solid grey;
padding: 5px;
height: 300px;
width: 500px;
overflow: auto;
}
<button type="button" onClick="autoPlay()">
GO
</button>
<pre id="val" contenteditable="true" spellcheck="false">
AL PAG31503XZ 2351 ESB ABC AR PAG30079XZ 2213 ESB ABC
BL PMC16410R7 1798 ESB ABC BR PMC27936XZ 2444 ESB ABC
CL PMC27377XZ 2563 ESB ABC CR PMC21402XZ 2622 ESB ABC
DL PMC24300XZ 2404 ESB ABC DR PMC26742XZ 2446 ESB ABC
EL PMC23546XZ 2682 ESB ABC ER PMC27450XZ 2774 ESB ABC
FL PMC22126C6 3022 ESB ABC FR PMC21220XZ 3292 ESB ABC
GL PMC21077XZ 2982 ESB ABC GR PMC28748XZ 2998 ESB ABC
HL PMC26051XZ 2492 ESB ABC HR PMC53510R7 2928 ESB ABC
JL PMC24177XZ 3014 ESB ABC JR PMC21444XZ 2873 ESB ABC
KL PMC24466XZ 2781 ESB ABC KR PMC21778XZ 2513 ESB ABC
LL PMC22897XZ 2454 ESB ABC LR PMC26473XZ 2778 ESB ABC
ML PMC27854XZ 2824 ESB ABC MR PMC26001XZ 3218 ESB ABC
PL PAJ1129 XZ 2279 ESB ABC PR PAJ51477XZ 2398 ESB ABC
RC PMC58993R7 2176 ESB ABC
LOWER DECK
FWD
11P PMC28357XZ 1122 ESB ABC
12P PMC24753XZ 1589 ESB ABC
13P PMC26671XZ 1862 ESB ABC
21P PMC28016XZ 3386 ESB ABC
</pre>
问题是您的第 3 个正则表达式中的 \ in \d 缺少转义。看看:
function autoPlay(){
var arr = [
["/ +/g","/"],
["/(?=[A-Z]R/)/g","$&\n"],
["/(\d{1,5})/([A-Z]{3})/g","\/"]
];
var arrL = arr.length;
var str = document.getElementById('val');
for (var i = 0; i < arrL; i++) {
var parts = /\/(.*)\/(.*)/.exec(arr[i][0]);
var restoredRegex = new RegExp(parts[1], parts[2]);
var result = str.innerHTML.replace(restoredRegex, arr[i][1]);
str.innerHTML = result
}
}
#val{
border: 1px solid grey;
padding: 5px;
height: 300px;
width: 500px;
overflow: auto;
}
<button type="button" onClick="autoPlay()">
GO
</button>
<pre id="val" contenteditable="true" spellcheck="false">
AL PAG31503XZ 2351 ESB ABC AR PAG30079XZ 2213 ESB ABC
BL PMC16410R7 1798 ESB ABC BR PMC27936XZ 2444 ESB ABC
CL PMC27377XZ 2563 ESB ABC CR PMC21402XZ 2622 ESB ABC
DL PMC24300XZ 2404 ESB ABC DR PMC26742XZ 2446 ESB ABC
EL PMC23546XZ 2682 ESB ABC ER PMC27450XZ 2774 ESB ABC
FL PMC22126C6 3022 ESB ABC FR PMC21220XZ 3292 ESB ABC
GL PMC21077XZ 2982 ESB ABC GR PMC28748XZ 2998 ESB ABC
HL PMC26051XZ 2492 ESB ABC HR PMC53510R7 2928 ESB ABC
JL PMC24177XZ 3014 ESB ABC JR PMC21444XZ 2873 ESB ABC
KL PMC24466XZ 2781 ESB ABC KR PMC21778XZ 2513 ESB ABC
LL PMC22897XZ 2454 ESB ABC LR PMC26473XZ 2778 ESB ABC
ML PMC27854XZ 2824 ESB ABC MR PMC26001XZ 3218 ESB ABC
PL PAJ1129 XZ 2279 ESB ABC PR PAJ51477XZ 2398 ESB ABC
RC PMC58993R7 2176 ESB ABC
LOWER DECK
FWD
11P PMC28357XZ 1122 ESB ABC
12P PMC24753XZ 1589 ESB ABC
13P PMC26671XZ 1862 ESB ABC
21P PMC28016XZ 3386 ESB ABC
</pre>
如果您尝试在该行之后执行控制台日志:
var parts = /\/(.*)\/(.*)/.exec(arr[i][0]);
您会看到在第 3 遍中返回的部分是:"(d{1,5})/([A-Z]{3})"
和 "g"
更好的是,如果你做一个简单的 console.log("/(\d{1,5})/([A-Z]{3})/g")
你会得到答案:"/(d{1,5})/([A-Z]{3})/g"
这是因为在您使用第一个正则表达式处理它之前,js 将 \d 视为转义字符在字符串级别。所以只需重新转义 \d (\\d) 就大功告成了。
此代码的更好版本是:
function autoPlay(){
var arr = [
["/ +/g","/"],
["/(?=[A-Z]R/)/g","$&\n"],
["/(\d{1,5})/([A-Z]{3})/g","\/"]
];
var str = document.getElementById('val');
arr.map((re)=>{
var parts = /\/(.*)\/(.*)/.exec(re[0]);
str.innerHTML = str.innerHTML.replace(
new RegExp(parts[1], parts[2]),
re[1]);
})
}
#val{
border: 1px solid grey;
padding: 5px;
height: 300px;
width: 500px;
overflow: auto;
}
<button type="button" onClick="autoPlay()">
GO
</button>
<pre id="val" contenteditable="true" spellcheck="false">
AL PAG31503XZ 2351 ESB ABC AR PAG30079XZ 2213 ESB ABC
BL PMC16410R7 1798 ESB ABC BR PMC27936XZ 2444 ESB ABC
CL PMC27377XZ 2563 ESB ABC CR PMC21402XZ 2622 ESB ABC
DL PMC24300XZ 2404 ESB ABC DR PMC26742XZ 2446 ESB ABC
EL PMC23546XZ 2682 ESB ABC ER PMC27450XZ 2774 ESB ABC
FL PMC22126C6 3022 ESB ABC FR PMC21220XZ 3292 ESB ABC
GL PMC21077XZ 2982 ESB ABC GR PMC28748XZ 2998 ESB ABC
HL PMC26051XZ 2492 ESB ABC HR PMC53510R7 2928 ESB ABC
JL PMC24177XZ 3014 ESB ABC JR PMC21444XZ 2873 ESB ABC
KL PMC24466XZ 2781 ESB ABC KR PMC21778XZ 2513 ESB ABC
LL PMC22897XZ 2454 ESB ABC LR PMC26473XZ 2778 ESB ABC
ML PMC27854XZ 2824 ESB ABC MR PMC26001XZ 3218 ESB ABC
PL PAJ1129 XZ 2279 ESB ABC PR PAJ51477XZ 2398 ESB ABC
RC PMC58993R7 2176 ESB ABC
LOWER DECK
FWD
11P PMC28357XZ 1122 ESB ABC
12P PMC24753XZ 1589 ESB ABC
13P PMC26671XZ 1862 ESB ABC
21P PMC28016XZ 3386 ESB ABC
</pre>
我成功了。
问题是由于 \d.
function autoPlay(){
var arr = [
[" +", "g" ,"/"],
["(?=[A-Z]R/)", "g","$&\n"],
["([0-9]{1,5})\/([A-Z]{3})" ,"g", "/"]
];
var arrL = arr.length;
var str = document.getElementById('val');
for (var i = 0; i < arrL; i++) {
var restoredRegex = new RegExp(arr[i][0], arr[i][1]);
var result = str.innerHTML.replace(restoredRegex, arr[i][2]);
str.innerHTML = result
}
}
#val{
border: 1px solid grey;
padding: 5px;
height: 300px;
width: 500px;
overflow: auto;
}
<button type="button" onClick="autoPlay()">
GO
</button>
<pre id="val" contenteditable="true" spellcheck="false">
AL PAG31503XZ 2351 ESB ABC AR PAG30079XZ 2213 ESB ABC
BL PMC16410R7 1798 ESB ABC BR PMC27936XZ 2444 ESB ABC
CL PMC27377XZ 2563 ESB ABC CR PMC21402XZ 2622 ESB ABC
DL PMC24300XZ 2404 ESB ABC DR PMC26742XZ 2446 ESB ABC
EL PMC23546XZ 2682 ESB ABC ER PMC27450XZ 2774 ESB ABC
FL PMC22126C6 3022 ESB ABC FR PMC21220XZ 3292 ESB ABC
GL PMC21077XZ 2982 ESB ABC GR PMC28748XZ 2998 ESB ABC
HL PMC26051XZ 2492 ESB ABC HR PMC53510R7 2928 ESB ABC
JL PMC24177XZ 3014 ESB ABC JR PMC21444XZ 2873 ESB ABC
KL PMC24466XZ 2781 ESB ABC KR PMC21778XZ 2513 ESB ABC
LL PMC22897XZ 2454 ESB ABC LR PMC26473XZ 2778 ESB ABC
ML PMC27854XZ 2824 ESB ABC MR PMC26001XZ 3218 ESB ABC
PL PAJ1129 XZ 2279 ESB ABC PR PAJ51477XZ 2398 ESB ABC
RC PMC58993R7 2176 ESB ABC
LOWER DECK
FWD
11P PMC28357XZ 1122 ESB ABC
12P PMC24753XZ 1589 ESB ABC
13P PMC26671XZ 1862 ESB ABC
21P PMC28016XZ 3386 ESB ABC
</pre>
P.S - 由于 \d 而造成问题。 (之前不知道为什么会这样@
Nelson Teixeira 的回答,所以我通过将 \d 替换为 [0-9] 来使其与一些 Juggad
一起工作。
我正在尝试对字符串执行一些替换操作。
替换操作存储在一个数组中。
一切顺利,直到第 3 个动作。
我无法让那个工作,也不明白为什么。
它不会给出错误,但不会执行最后一次替换
"/(\d{1,5})/([A-Z]{3})/g","/"
第一个替换用斜杠替换所有空格
第二个替换向下移动右侧的字符串(插入新行)
第 3 次替换应将 ESB
字符串与其前面的 4 位数字交换
例如
AR/PAG30079XZ/2213/ESB/ABC
会变成
AR/PAG30079XZ/ESB/2213/ABC
这是最后一部分不起作用
这是我目前得到的:
function autoPlay(){
var arr = [
["/ +/g","/"],
["/(?=[A-Z]R/)/g","$&\n"],
["/(\d{1,5})/([A-Z]{3})/g","/"]
];
var arrL = arr.length;
var str = document.getElementById('val');
for (var i = 0; i < arrL; i++) {
var parts = /\/(.*)\/(.*)/.exec(arr[i][0]);
var restoredRegex = new RegExp(parts[1], parts[2]);
var result = str.innerHTML.replace(restoredRegex, arr[i][1]);
str.innerHTML = result
}
}
#val{
border: 1px solid grey;
padding: 5px;
height: 300px;
width: 500px;
overflow: auto;
}
<button type="button" onClick="autoPlay()">
GO
</button>
<pre id="val" contenteditable="true" spellcheck="false">
AL PAG31503XZ 2351 ESB ABC AR PAG30079XZ 2213 ESB ABC
BL PMC16410R7 1798 ESB ABC BR PMC27936XZ 2444 ESB ABC
CL PMC27377XZ 2563 ESB ABC CR PMC21402XZ 2622 ESB ABC
DL PMC24300XZ 2404 ESB ABC DR PMC26742XZ 2446 ESB ABC
EL PMC23546XZ 2682 ESB ABC ER PMC27450XZ 2774 ESB ABC
FL PMC22126C6 3022 ESB ABC FR PMC21220XZ 3292 ESB ABC
GL PMC21077XZ 2982 ESB ABC GR PMC28748XZ 2998 ESB ABC
HL PMC26051XZ 2492 ESB ABC HR PMC53510R7 2928 ESB ABC
JL PMC24177XZ 3014 ESB ABC JR PMC21444XZ 2873 ESB ABC
KL PMC24466XZ 2781 ESB ABC KR PMC21778XZ 2513 ESB ABC
LL PMC22897XZ 2454 ESB ABC LR PMC26473XZ 2778 ESB ABC
ML PMC27854XZ 2824 ESB ABC MR PMC26001XZ 3218 ESB ABC
PL PAJ1129 XZ 2279 ESB ABC PR PAJ51477XZ 2398 ESB ABC
RC PMC58993R7 2176 ESB ABC
LOWER DECK
FWD
11P PMC28357XZ 1122 ESB ABC
12P PMC24753XZ 1589 ESB ABC
13P PMC26671XZ 1862 ESB ABC
21P PMC28016XZ 3386 ESB ABC
</pre>
问题是您的第 3 个正则表达式中的 \ in \d 缺少转义。看看:
function autoPlay(){
var arr = [
["/ +/g","/"],
["/(?=[A-Z]R/)/g","$&\n"],
["/(\d{1,5})/([A-Z]{3})/g","\/"]
];
var arrL = arr.length;
var str = document.getElementById('val');
for (var i = 0; i < arrL; i++) {
var parts = /\/(.*)\/(.*)/.exec(arr[i][0]);
var restoredRegex = new RegExp(parts[1], parts[2]);
var result = str.innerHTML.replace(restoredRegex, arr[i][1]);
str.innerHTML = result
}
}
#val{
border: 1px solid grey;
padding: 5px;
height: 300px;
width: 500px;
overflow: auto;
}
<button type="button" onClick="autoPlay()">
GO
</button>
<pre id="val" contenteditable="true" spellcheck="false">
AL PAG31503XZ 2351 ESB ABC AR PAG30079XZ 2213 ESB ABC
BL PMC16410R7 1798 ESB ABC BR PMC27936XZ 2444 ESB ABC
CL PMC27377XZ 2563 ESB ABC CR PMC21402XZ 2622 ESB ABC
DL PMC24300XZ 2404 ESB ABC DR PMC26742XZ 2446 ESB ABC
EL PMC23546XZ 2682 ESB ABC ER PMC27450XZ 2774 ESB ABC
FL PMC22126C6 3022 ESB ABC FR PMC21220XZ 3292 ESB ABC
GL PMC21077XZ 2982 ESB ABC GR PMC28748XZ 2998 ESB ABC
HL PMC26051XZ 2492 ESB ABC HR PMC53510R7 2928 ESB ABC
JL PMC24177XZ 3014 ESB ABC JR PMC21444XZ 2873 ESB ABC
KL PMC24466XZ 2781 ESB ABC KR PMC21778XZ 2513 ESB ABC
LL PMC22897XZ 2454 ESB ABC LR PMC26473XZ 2778 ESB ABC
ML PMC27854XZ 2824 ESB ABC MR PMC26001XZ 3218 ESB ABC
PL PAJ1129 XZ 2279 ESB ABC PR PAJ51477XZ 2398 ESB ABC
RC PMC58993R7 2176 ESB ABC
LOWER DECK
FWD
11P PMC28357XZ 1122 ESB ABC
12P PMC24753XZ 1589 ESB ABC
13P PMC26671XZ 1862 ESB ABC
21P PMC28016XZ 3386 ESB ABC
</pre>
如果您尝试在该行之后执行控制台日志:
var parts = /\/(.*)\/(.*)/.exec(arr[i][0]);
您会看到在第 3 遍中返回的部分是:"(d{1,5})/([A-Z]{3})"
和 "g"
更好的是,如果你做一个简单的 console.log("/(\d{1,5})/([A-Z]{3})/g")
你会得到答案:"/(d{1,5})/([A-Z]{3})/g"
这是因为在您使用第一个正则表达式处理它之前,js 将 \d 视为转义字符在字符串级别。所以只需重新转义 \d (\\d) 就大功告成了。
此代码的更好版本是:
function autoPlay(){
var arr = [
["/ +/g","/"],
["/(?=[A-Z]R/)/g","$&\n"],
["/(\d{1,5})/([A-Z]{3})/g","\/"]
];
var str = document.getElementById('val');
arr.map((re)=>{
var parts = /\/(.*)\/(.*)/.exec(re[0]);
str.innerHTML = str.innerHTML.replace(
new RegExp(parts[1], parts[2]),
re[1]);
})
}
#val{
border: 1px solid grey;
padding: 5px;
height: 300px;
width: 500px;
overflow: auto;
}
<button type="button" onClick="autoPlay()">
GO
</button>
<pre id="val" contenteditable="true" spellcheck="false">
AL PAG31503XZ 2351 ESB ABC AR PAG30079XZ 2213 ESB ABC
BL PMC16410R7 1798 ESB ABC BR PMC27936XZ 2444 ESB ABC
CL PMC27377XZ 2563 ESB ABC CR PMC21402XZ 2622 ESB ABC
DL PMC24300XZ 2404 ESB ABC DR PMC26742XZ 2446 ESB ABC
EL PMC23546XZ 2682 ESB ABC ER PMC27450XZ 2774 ESB ABC
FL PMC22126C6 3022 ESB ABC FR PMC21220XZ 3292 ESB ABC
GL PMC21077XZ 2982 ESB ABC GR PMC28748XZ 2998 ESB ABC
HL PMC26051XZ 2492 ESB ABC HR PMC53510R7 2928 ESB ABC
JL PMC24177XZ 3014 ESB ABC JR PMC21444XZ 2873 ESB ABC
KL PMC24466XZ 2781 ESB ABC KR PMC21778XZ 2513 ESB ABC
LL PMC22897XZ 2454 ESB ABC LR PMC26473XZ 2778 ESB ABC
ML PMC27854XZ 2824 ESB ABC MR PMC26001XZ 3218 ESB ABC
PL PAJ1129 XZ 2279 ESB ABC PR PAJ51477XZ 2398 ESB ABC
RC PMC58993R7 2176 ESB ABC
LOWER DECK
FWD
11P PMC28357XZ 1122 ESB ABC
12P PMC24753XZ 1589 ESB ABC
13P PMC26671XZ 1862 ESB ABC
21P PMC28016XZ 3386 ESB ABC
</pre>
我成功了。
问题是由于 \d.
function autoPlay(){
var arr = [
[" +", "g" ,"/"],
["(?=[A-Z]R/)", "g","$&\n"],
["([0-9]{1,5})\/([A-Z]{3})" ,"g", "/"]
];
var arrL = arr.length;
var str = document.getElementById('val');
for (var i = 0; i < arrL; i++) {
var restoredRegex = new RegExp(arr[i][0], arr[i][1]);
var result = str.innerHTML.replace(restoredRegex, arr[i][2]);
str.innerHTML = result
}
}
#val{
border: 1px solid grey;
padding: 5px;
height: 300px;
width: 500px;
overflow: auto;
}
<button type="button" onClick="autoPlay()">
GO
</button>
<pre id="val" contenteditable="true" spellcheck="false">
AL PAG31503XZ 2351 ESB ABC AR PAG30079XZ 2213 ESB ABC
BL PMC16410R7 1798 ESB ABC BR PMC27936XZ 2444 ESB ABC
CL PMC27377XZ 2563 ESB ABC CR PMC21402XZ 2622 ESB ABC
DL PMC24300XZ 2404 ESB ABC DR PMC26742XZ 2446 ESB ABC
EL PMC23546XZ 2682 ESB ABC ER PMC27450XZ 2774 ESB ABC
FL PMC22126C6 3022 ESB ABC FR PMC21220XZ 3292 ESB ABC
GL PMC21077XZ 2982 ESB ABC GR PMC28748XZ 2998 ESB ABC
HL PMC26051XZ 2492 ESB ABC HR PMC53510R7 2928 ESB ABC
JL PMC24177XZ 3014 ESB ABC JR PMC21444XZ 2873 ESB ABC
KL PMC24466XZ 2781 ESB ABC KR PMC21778XZ 2513 ESB ABC
LL PMC22897XZ 2454 ESB ABC LR PMC26473XZ 2778 ESB ABC
ML PMC27854XZ 2824 ESB ABC MR PMC26001XZ 3218 ESB ABC
PL PAJ1129 XZ 2279 ESB ABC PR PAJ51477XZ 2398 ESB ABC
RC PMC58993R7 2176 ESB ABC
LOWER DECK
FWD
11P PMC28357XZ 1122 ESB ABC
12P PMC24753XZ 1589 ESB ABC
13P PMC26671XZ 1862 ESB ABC
21P PMC28016XZ 3386 ESB ABC
</pre>
P.S - 由于 \d 而造成问题。 (之前不知道为什么会这样@
Nelson Teixeira 的回答,所以我通过将 \d 替换为 [0-9] 来使其与一些 Juggad
一起工作。