I have an array of strings, and I want to create a new array which contains the same strings without port numbers (the port number is a ":" followed by a number). For example if the string is "http://www.example.com:8080/hello/" Then it should be replaced with "http://www.example.com/hello/". How do I do it in JavaScript? I need it to call safari.extension.addContentScriptFromURL because the whitelist can't contain port numbers. If possible, it's better to replace the port number only between the second and third slash and leave the rest of the string unchanged. asked Sep 11, 2014 at 9:44
1 One quite nifty way to do this, is to create an
http://jsfiddle.net/pdymeb5d/ answered Sep 11, 2014 at 10:13
CBroeCBroe 88.1k13 gold badges89 silver badges142 bronze badges This should probably do what you want:
Edit: It seems the schema part of URIs can contain "+", "." and "-" also. Changed the regular expression accordingly. See: https://en.wikipedia.org/wiki/URI_scheme answered Sep 11, 2014 at 10:04
0 You can use regex with string.replace() as follows,
answered Sep 11, 2014 at 10:13
1
Use this to parse any URL and arrange in a format you prefer. answered Sep 11, 2014 at 10:02
Subash SelvarajSubash Selvaraj 3,3871 gold badge13 silver badges17 bronze badges OK, I used your answer and changed it a little, because the protocol may contain dashes too:
answered Sep 11, 2014 at 10:22
UriUri 2,5398 gold badges37 silver badges78 bronze badges I have found best solution here.
Better to read full page. remove hostname and port from url using regular expression answered May 9, 2018 at 10:59
Asad NaeemAsad Naeem 5208 silver badges12 bronze badges |