简体   繁体   中英

How can I remove dynamic data in string before a value in that string? / using Lodash's _.trimStart with dynamic data?

I'm working urls returned from a server that I have no control over where and sometimes the urls return with extra data at the front. For instance

sometimes it returns this

https://example.com/image/5119b3905.jpg

and this I can use, but sometimes it will return something like this

https://d1yww.cloudfront.net/9MA=/670x670/example.com/image/5119b3905.jpg

where I'd like to use remove everything before the example.com and to do that I could use something like lodash's _.trimStart method something like

    _.trimStart('https://d1yww.cloudfront.net/9MA=/670x670/example.com/image/5119b3905.jpg',
   'd1yww.cloudfront.net/9MA=/670x670');

but the d1yww.cloudfront.net/9MA=/670x670' is never static for me to do this and I don't know how to grab the dynamic data to use _.trimStart and I don't see any other useful lodash's methods and I don't know of any vanilla javascript ones.

TLDR: How can I remove dynamic data in string before a value in that string (in this example everything before the example.com )

You don't need lodash to do that

var str = 'https://d1yww.cloudfront.net/9MA=/670x670/example.com/image/5119b3905.jpg'
str.substr(str.indexOf('example.com'))

You could search for a Regular Expression

For Example :

/\\/([-a-zA-Z0-9@:%._\\+~#=]{2,256}\\.[az]{2,6}\\b)\\//g

and look for the second match

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM