Home > Enterprise >  extract url and name attributes from the given string
extract url and name attributes from the given string

Time:04-01

the format of the input string is >>

[https://thisisurl.com] This is Name

how to extract "https://thisisurl.com", and "This is url" attributes from it

where the url attribute is given in brackets [???] and remaining text is the name attribute

I want a function that can do this task for me

CodePudding user response:

You can use escape character \ for this as follow:

const str = '[https://thisisurl.com] This is Name'

const regex = /\[(.*)\] (.*)/i
const matchResult = str.match(regex)

const url = matchResult[1]
const name = matchResult[2]

console.log(`url: "${url}" name: "${name}"`)

  • Related