不允许使用八进制转义序列:

问题描述 投票:0回答:1
const str = `        Name: FirstName LastName
        123-45-6789
        987-65-4321
        Address: 1234 abc Street, Apt. 567, Ricefield, ILE 602701
        Phone: (123) 456-7890
        \t
        Email: [email protected]
        URL: https://www.example.com
        \f 
        IP: 192.168.1.1
        Date: 01/01/2023
        Price: $99.99
        \r 
        Color: Red, Blue, Green, Yellow
        Mixed: abc123XYZ987
        \0 \v
        \141pple \x42anana \u0063herry
        
`;
 let regexOctal = /\141/;
 console.log("regexOctal.exec(str) : ", regexOctal.exec(str));

错误:苹果

我想看看八进制,十六进制和unicode如何与正则表达式一起搜索

我尝试在模板字符串中使用 ${} 以及在没有模板字符串的新变量中使用 ${} 但仍然错误 1)

const str = `        Name: FirstName LastName
        123-45-6789
        987-65-4321
        Address: 1234 abc Street, Apt. 567, Ricefield, ILE 602701
        Phone: (123) 456-7890
        \t
        Email: [email protected]
        URL: https://www.example.com
        \f 
        IP: 192.168.1.1
        Date: 01/01/2023
        Price: $99.99
        \r 
        Color: Red, Blue, Green, Yellow
        Mixed: abc123XYZ987
        \0 \v
        ${"\141"}pple \x42anana \u0063herry
`;

2)

const newStr = "\141pple \x42anana \u0063herry";

javascript regex string octal
1个回答
0
投票

要在 Code 中获取小写 a,您需要使用其代码编码 (U+0061):

const regex = /\141/gm;

const str = `\u{0061}pple`;

// Reset `lastIndex` if this regex is defined globally
// regex.lastIndex = 0;

let m;

while ((m = regex.exec(str)) !== null) {
    // This is necessary to avoid infinite loops with zero-width matches
    if (m.index === regex.lastIndex) {
        regex.lastIndex++;
    }
    
    // The result can be accessed through the `m`-variable.
    m.forEach((match, groupIndex) => {
        console.log(`Found match, group ${groupIndex}: ${match}`);
    });
}

© www.soinside.com 2019 - 2024. All rights reserved.