Javascript 多个正则表达式常量-为每个匹配的单词添加样式

Javascript 多个正则表达式常量-为每个匹配的单词添加样式,javascript,regex,replace,Javascript,Regex,Replace,我必须使用一个基本的查找字符串并添加样式,例如${match},但我想对下面所有四个正则表达式匹配应用相同的方法;匹配相应的.css样式 orders: example: 975612345678. range: 975600000000-975699999999 customers: example: 1712345678901. range: 1700000000000-1799999999999 emails: example: word@test.com, 01234@nu

我必须使用一个基本的查找字符串并添加样式,例如${match},但我想对下面所有四个正则表达式匹配应用相同的方法;匹配相应的.css样式

orders:    example: 975612345678. range: 975600000000-975699999999 
customers: example: 1712345678901. range: 1700000000000-1799999999999
emails:    example: word@test.com, 01234@numbers.tk
items:     example: 32101234, 1012345678. range: 00000000-99999999, range: 1000000000-1099999999
我们可以看到,它忽略了电子邮件和项目的正则表达式:范围:00000000-9999999

const orders = /\b9756[0-9]{8}\b/;
const customers = /\b17[0-9]{11}\b/;
const emails = ??
const items = ?? and /\b10[0-9]{8}\b/;
有人能解决这个问题吗?我在上找到了一些东西,但还没有找到有效的解决方案。
可以在

查看演示。这里有一个重构,它应该可以满足您的需要

我正在使用多个替换,第一个处理电子邮件,第二个处理不属于电子邮件地址的数字

不幸的是,它不一定是性能最好的代码,但它适用于给定的输入

来自的电子邮件正则表达式,因为电子邮件对于正则表达式来说非常复杂

此处为原始代码段:

var list=document.getelementsbyclassname消息内容 //window.copyData=copyData; //使用数字操纵来检查订单/客户/项目是否为实际值,因为我们不需要正则表达式,因为它们只是范围。如果它们更复杂,那么我们可以用正则表达式替换它 函数同序串{ 返回Numberstring>=9756E8&&Numberstring<9757E8; } 函数是CustomerString{ 返回Numberstring>=17E11&&Numberstring<18E11; } 函数isItemstring{ 返回Numberstring>=0&&Numberstring<1e8 | | Numberstring>=1E9&&Numberstring<1.1E9 } 对于var i=0;i{ 如果是同序的{ 返回`${match}` }否则,如果客户匹配{ 返回`${match}` }否则,如果匹配{ 返回`${match}` }否则{ 复赛; } } } 函数copyDatae{ const textarea=document.createElement'textarea'; textarea.textContent=e.currentTarget.innerText; textarea.style.position='fixed';//防止在MS Edge中滚动到页面底部。 document.body.appendChildtextarea; textarea.select; document.execCommand'copy'; document.body.removeChildtextarea; } 用于document.querySelectorAll.copyData的常量{ 元素添加列表器“单击”,复制数据 } div{ 空白:预处理; } .命令{ 背景颜色:绿色; } /*相应的regex订单、客户、电子邮件和项目需要匹配样式*/ .顾客{ 背景色:红色; } .电子邮件{ 背景颜色:蓝色; } .项目{ 背景颜色:黄色; } 订单:例如:975612345678。范围:975600000000-9756999999 客户:例如:1712345678901。范围:1700000000000-179999999 电子邮件:例如:word@test.com, 01234@numbers.tk 项目:示例:321012341012345678。范围:00000000-9999999,范围:100000000-109999999 好: 订单:例如:975612345678和140123456789 客户:例如:1712345678901。 电子邮件:例如:word@test.com 项目:示例:32101234和1012345678 错: 订单:示例:975612345678测试和测试140123456789 客户:例如:1712345678901测试和1712345678901测试 电子邮件:例如:word@test.comtest 项目:示例:32101234测试和测试1012345678
啊,太好了!也可以使按钮重定向到某个URL。比如说,当我只点击按钮项目32101234将其链接到并复制时,我只看到一个小“错误”,一个单词或字母意外地粘在一个数字上。例如,不小心忘记了一个空格@RRG,我已经解决了你所问的问题。我让这些项目有一个指向谷歌搜索页面的链接,但由于权限问题,它无法处理堆栈溢出。啊,很好,谢谢!小东西。如何删除字符串中的所有点,如12.34.5678->12345678 in items->return Numberstring>=0&&Numberstring<1e8和正则表达式,以删除客户1712 3456 7890 1->1712345678901中的空格。我认为,事后来看,从所有值中删除所有空格和点也是可能的。
for (var i = 0; i < list.length; i++) {
    let text = list[i].textContent;
    // Make the regex have boundary characters to ensure that it's checking against the whole number, rather than a part. example: 975612345678. range: 975600000000-975699999999  
    const orders = /\b9756[0-9]{8}\b/; 
    list[i].innerHTML = text.replace(
        // Replace all number sequences in the text
        /\d+/g,
        // Replace by checking if the replacement text matches "const orders"(regex) to determine color
        (match) => `<button onclick="copyData(event)" class="${orders.test(match)}">${match}</button>`
    )
}
/*.true needs to be .orders */
.true {
  background-color: green;
}
.orders {
    background-color: green;
}

/* corresponding regex "orders, customers, emails and items" needs to match style */
.customers {
    background-color: red;
}
.emails {
    background-color: blue;
}
.items {
    background-color: yellow;
}