简体   繁体   中英

Fixing/cleaning user-provided URLs via Javascript

Does anyone happen to have javascript code focused on cleaning up user-provided URLs? I have a mobile app that asks users to enter their website URL, and many times the URLs are not properly formatted, lack the http:// prefix, etc. (Typing on mobile tends to be error prone) I often see stuff like:

google.com
ww.google.com
http//www.goggle.com
http://www..google.com

I know there'd be a certain amount of assuming one would have to do to clean a URL, but it just seems like there would be a library out there somewhere for URL cleaning?

I guess this is as best of a start as you can get. It now deals with: .co.uk or whatever multi-part domain types, protocol, www-less, protocol-less etc. I plan to make it a library plugin.

https://github.com/WebSudosUK/framework/blob/master/com.websudos.urlMatcher.js

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