-6

I have to run a JavaScript regex on URLs to detect any (one or more) special character after URL and put space before them, like:

url, --> url , or url), --> url ),

e.g.

(https://www.microsoft.com), into (https://www.microsoft.com ),

halfer
  • 19,824
  • 17
  • 99
  • 186
Ashz
  • 23
  • 5
  • Why do you want to do that? –  Dec 09 '14 at 03:04
  • 1
    And what have you tried? – Ken Herbert Dec 09 '14 at 03:05
  • I want becz url in my Doc with special char in end is not clickable, so adding one more space will solve my issue. – Ashz Dec 09 '14 at 03:13
  • @Ashz what was the regex you used for matching URL's? – Avinash Raj Dec 09 '14 at 03:16
  • i don't have one i need to make, i would be great help if you can help in building the expression. – Ashz Dec 09 '14 at 03:28
  • see here https://regex101.com/r/eX9yA4/1 – Avinash Raj Dec 09 '14 at 03:40
  • Someone please help me with (JavaScript RegEx) i was trying to put one space ,when and only when there is one special character is there before url , like below example. ,www.google.com -->> , www.google.com ,https://www.google.com -->> , https://www.google.com It shouldnt give space when its just only url no Special char. There can be one or more character before url -- Please help me ,i tried searching for exact question/topic but didnt find my answer ,so hopefuly not asking duplicate question. – Ashz Jan 20 '15 at 13:39

1 Answers1

0
var myregexp =/(\b(https?|ftp|file|http):\/\/[\-A-Za-z0-9+&@#\/%?=~_|!:,.;]*[\-A-Za-z0-9+&@#\/%=~_|])/g;
var str="http://www.microsoft.com)"; //url
var res=myregexp.exec(str);          //execute regex query  
str=str.substring(0,myregexp.lastIndex)+' '+str.substring(myregexp.lastIndex);

I used Regex query for url from : https://stackoverflow.com/a/163684

Community
  • 1
  • 1
chiragjn
  • 764
  • 11
  • 31
  • Wondeful is worked for me , can you please also help me what else i need to add if i also need to remove protocol from url e.g. "(https://www.google.com), convert to (www.google.com )," as in this requirement , space before comma issue is solved by your solution ,can you please also help me with removal of protocol ? – Ashz Dec 09 '14 at 05:30
  • Many Thanks !!, is worked for me , can you please also help me what else i need to do to ,if i also need to remove protocol from url if url has "www." e.g. "https://www.google.com" , and keep it intact if it didnt have "www." e.g. "https://google.com" ,............... Case1 : "(https://www.google.com), convert to (www.google.com )," BUT IN Case2: "(https://google.com), convert to (https://google.com )," As in Case1 it removed https and added space before ")," but in Case2 it only added space before ")," and didnt removed "https://" from url. – Ashz Dec 09 '14 at 05:59
  • Update Regarding Above ,its only working for first url in string doesnt works if many url in string. – Ashz Dec 09 '14 at 06:30
  • http://ideone.com/ESKVH1 this extracts and gives the url without any special characters or whitespaces at start and end – chiragjn Dec 09 '14 at 06:59
  • keep executing the regex in loop to get next matches A good guide :https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/exec – chiragjn Dec 09 '14 at 07:02