javascript - 如何使用正则表达式根据字符串过滤数组项?

标签 javascript regex url ecmascript-6

filter() 的作用是从数组中删除与 URL 匹配的 currentGroupMalappuram/123456/12 之后是否有数字并不重要。 ES6 有没有最好的方法?

演示:https://jsbin.com/jozuqameto/edit?js,console

const initialLinks = [
  "http://www.lchfmalayalam.com",
  "https://t.me/Malappuram",
  "https://t.me/keraladevelopers/42716",
  "http://www.whatsapp.com",
  "https://www.youtube.com/watch?v=BnbFRSyHIl4",
  "http://google.com",
  "https://t.me/joinchat/NHNd1hcSMCoYlnZGSC_H7g",
  "https://t.me/keraladevelopers/",
  "http://t.me/keraladevelopers",
  "http://athimannil.com/react/",
  "http://athimannil.info/",
  "https://t.me/hellomates/5",
  "http://t.me/Malappuram/32156",
  "http://t.me/keraladevelopers/42716",
  "http://t.me/joinchat/NHNd1hcSMCoYlnZGSC_H7g",
  "http://t.me/keraladevelopers/",
  "http://t.me/hellomates/5"
];

const normalizeTme = R.replace(
  /^(?:@|(?:https?:\/\/)?(?:t\.me|telegram\.(?:me|dog))\/)(\w+)(\/.+)?/i,
  (_match, username, rest) => {
    return /^\/\d+$/.test(rest) ?
      `https://t.me/${username.toLowerCase()}` :
      `https://t.me/${username.toLowerCase()}${rest || ""}`;
  }
);

const filterOwnLinks = groupUsername => {
  return R.match(
    /^(?:@|(?:https?:\/\/)?(?:t\.me|telegram\.(?:me|dog))\/)(\w+)(\/.+)?/i,
    (_match, username, rest) => {
      if (username) {
        return currentGroup.toLowerCase() !== username.toLowerCase();
      }
      return true;
    }
  );
};

const currentGroup = "Malappuram";

const urls = R.uniq(initialLinks)
  .filter(filterOwnLinks)
  .map(normalizeTme);

console.log(initialLinks);
console.log(urls);
<script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.25.0/ramda.min.js"></script>

最佳答案

您可以使用URL api来解析url,并从解析url对象中获取路径名,并检查是否以currentGroup开头

const initialLinks = ["http://www.lchfmalayalam.com","https://t.me/Malappuram","https://t.me/keraladevelopers/42716","http://www.whatsapp.com","https://www.youtube.com/watch?v=BnbFRSyHIl4","http://google.com","https://t.me/joinchat/NHNd1hcSMCoYlnZGSC_H7g","https://t.me/keraladevelopers/","http://t.me/keraladevelopers","http://athimannil.com/react/","http://athimannil.info/","https://t.me/hellomates/5","http://t.me/Malappuram/32156","http://t.me/keraladevelopers/42716","http://t.me/joinchat/NHNd1hcSMCoYlnZGSC_H7g","http://t.me/keraladevelopers/","http://t.me/hellomates/5"];
const currentGroup = "Malappuram";
const urls = [...new Set(initialLinks)]

let final = urls.filter(url => {
  let parsed = new URL(url)
  let pattern = new RegExp(`^\/${currentGroup}`,'i')
  return !pattern.test(parsed.pathname)
})

console.log(final)

关于javascript - 如何使用正则表达式根据字符串过滤数组项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57411673/

相关文章:

javascript - 是否可以仅针对 typescript 上的特定类型扩展 Array.prototype ?

javascript - Google map - 未通过 &lt;iframe&gt; 加载

javascript - 如何将字符串中的 url 替换为 anchor 标记

javascript - 使用 javascript 中的通用字母表的正则表达式验证输入

php - cakephp 分页更改 url 使它们对 seo 更友好

url - 从 URL 中提取值字符串

javascript - react 表与从/到映射

javascript - 无法测试去抖动的 Backbone View 事件

python - 使用正则表达式解析 Snort 警报文件

node.js - http请求的for循环延迟