const url = "./index.js mongodb://localhost:27017/docs find betty";
docs
是我的数据库名称
const db_name = ?
const url = ?
如何在一个变量中使用正则表达式分离数据库名称,在另一个变量中使用 url 来分隔数据库名称?
任何帮助都非常感谢。
使用组匹配来定义要捕获的字符:
const input = "./index.js mongodb://localhost:27017/docs find betty";
const regex = /(mongodb://w*(:d{1,5})?/(w*))/;
const url = input.match(regex)[1];
const db_name = input.match(regex)[3];
console.log(url);
console.log(db_name);