如何将PHP正则表达式转换为Node.js

gcuhipw9  于 2023-08-02  发布在  PHP
关注(0)|答案(4)|浏览(100)

我需要将php正则表达式转换为Node.js。我的PHP正则表达式代码:
第一个月
我是Nodejs的新手。我如何转换这个?

piok6c0g

piok6c0g1#

下面是等效的Node.js正则表达式

/\bhttps?:\/\/[^,\s()<>]+(?:\([\w\d]+\)|([^,\p{P}\s]|\/))/gu

字符串

演示和说明:https://regex101.com/r/Y2vlwe/1

5lhxktic

5lhxktic2#

const regex = /\bhttps?:\/\/[^,\s()<>]+(?:\([\w\d]+\)|([^,[:punct:]\s]|\/))/g;
const script = "Your input here";

const matches = script.match(regex);
console.log(matches);

字符串

7vhp5slm

7vhp5slm3#

const regex = /\bhttps?:\/\/[^,\s()<>]+(?:\([\w\d]+\)|([^,\p{P}\s]|\/))/gu;
const v = "https://www.google.com";
const v1 = "sampel text";

const matches = v.match(regex);   // matches
const matches1 = v1.match(regex); // Not matches
console.log(matches);
console.log(matches1);

字符串

gk7wooem

gk7wooem4#

我假设你正在尝试检查一个有效的URL。JavaScript中最简单的事情就是这样做:

let url = 'https://gooogle.com';

  try {
   const parts = new URL(url);
   console.log(parts);

  } catch(err) {
     console.error('not a valid url', err);
  }

字符串

相关问题