简体   繁体   中英

Javascript - breaking a string into a formula

let's say I have an image, with this formula:

blabla.com/bla/twofive.hash.png

The above can be represented as a string in JavaScript.

I'd like to know how to build an array that will break every special sign (such as "." and "/"), and will contain something like this:

var arr =  ['blabla', 'com', 'bla', 'twofive', 'hash', 'png'];

so I can easily access any property of the array with a simple arr[number];

试试吧

"blabla.com/bla/twofive.hash.png".split(/[./]/) //outputs ["blabla", "com", "bla", "twofive", "hash", "png"]

Try this:

'blabla.com/bla/twofive.hash.png'.split(/\.|\//); 

Result:

["blabla", "com", "bla", "twofive", "hash", "png"]

You can also use match()

 var res = "blabla.com/bla/twofive.hash.png".match(/[^./]+/g); console.log(res); 

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