-4
const url = "./index.js mongodb://localhost:27017/docs find betty";

docs is my database name

const db_name = ? 
const url = ? 

How do I separate out the database name using regex in one variable, and the url in another?

Any help is highly appreciated.

RToyo
  • 2,877
  • 1
  • 15
  • 22
Dee A
  • 19
  • 1
  • 3

1 Answers1

1

Use group matches to define the characters you want to capture:

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);
esqew
  • 42,425
  • 27
  • 92
  • 132