javascript url-safe filename-safe string

前端 未结 4 777
误落风尘
误落风尘 2021-01-30 10:08

Looking for a regex/replace function to take a user inputted string say, \"John Smith\'s Cool Page\" and return a filename/url safe string like \"john_smith_s_cool_page.html\",

4条回答
  •  花落未央
    2021-01-30 11:03

    For more flexible and robust handling of unicode characters etc, you could use the slugify in conjunction with some regex to remove unsafe URL characters

    const urlSafeFilename = slugify(filename, { remove: /"<>#%\{\}\|\\\^~\[\]`;\?:@=&/g });
    

    This produces nice kebab-case filenemas in your url and allows for more characters outside the a-z0-9 range.

提交回复
热议问题