Hi, Please find below a much much simpler version of what i'm trying to acomplish. In the real version, i'm pulling data from php using xmlhttp via Ajax. I tried to do preg_replace in php side but it was not sending over the html tags and embedded onclick even containing a javascript function properly. So I am now struggling to do this in Javascript. <html> <body> <script type="text/javascript"> var str="@Godfather @topdog @holyinsomniac This is a test"; var patt1=/@([^\s]+)/g; while (patt1.test(str)==true) { document.write(str.match(patt1.source)); } </script> </body> </html> Code (markup): I want to get the following results: Godfather, topdog, holyinsomniac I know this is possible and i'm pretty close....I think. I will really appreciate any help. Thank you!
I am not sure but i think JS is confused weather you want "\" and "s" as different character or weather "\s" represents a space. My suggestion would be to replace "\s" with " "(Plain Space). See if it works
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> <title>None</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <script type="text/javascript"> function init(){ var nStr = "@Godfather This is a test @topdog @holyinsomniac This is a test @holyinsomniac @Godfather @topdog This is a test"; nStr = nStr.replace(/@(\w+)\s?/g, "<$1,> ") .replace(/>([\w\s]+)<?/g, " ") .replace(/[,\s]+$|[<>]/g, ""); alert(nStr); } navigator.appName == "Microsoft Internet Explorer" ? attachEvent('onload', init, false) : addEventListener('load', init, false); </script> </head> <body> </body> </html> Code (markup):
Hello, thank you very much for providing your input. However, it is not working properly. See below a simplified version: <html> <body> <script type="text/javascript"> var str="@Godfather @topdog @holyinsomniac This is a test. Bull*** - ok, Noway"; var str=str.replace(/@(\w+)\s?/g, "<$1,> ") .replace(/>([\w\s]+)<?/g, " ") .replace(/[,\s]+$|[<>]/g, ""); alert (str); </script> </body> </html> Code (markup): When you test this, you will notice that Bull and ok is also showing up. My goal is to only get words that are followed by @.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> <title>None</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <script type="text/javascript"> function init(){ var nStr = "@Godfather @topdog @holyinsomniac This is a test. Bull*** - ok, Noway"; nStr = nStr.match(/@\w+/g); for (each in nStr) { nStr[each] = nStr[each].toString().replace(/@/, " "); } nStr = nStr.join(); alert(nStr); } navigator.appName == "Microsoft Internet Explorer" ? attachEvent('onload', init, false) : addEventListener('load', init, false); </script> </head> <body> </body> </html> Code (markup):
You're welcome. I did it that way at first, then for some reason I deleted it and did it the way that I originally posted.
I got around to testing this out some more but i need a little bit more help. See my function below that I am working with. function user_linkify(status_text) { alert (status_text); var status_message_to = status_text.match(/@\w+/g); //alert ("Status message to" + status_message_to); for (each in status_message_to) { var message_to_username = status_message_to[each].toString().replace(/@/, ''); alert ("message to username" + status_message_to[each]); status_message_to[each] = status_message_to[each].toString().replace(/@([^\s]+)/g, '@<a href=\"#\" class=\"user_wall_link\" onclick=\"javascript:getUserWall('$1');return false;\">$1</a>'); } status_text = status_message_to.join(); return status_text; } Code (markup): I am passing it a string value, something like this var status_text = "@holyinsomniac this should work now @mike what do you think? "; user_linkify(status_text); Code (markup): What I am getting right now is @holyinsomniac@mike with proper hyperlinks. My Question is, how can I keep the rest of the text as well? My goal is to have the original string "@holyinsomniac this should work now @mike what do you think?" BUT with hyperlinks.
It helps to not piecemeal a problem. You're going to have to define a start and an end, otherwise your *** Bull, this is Noway, would be included for the last @someone in your last example string. I used the vertical bar for the end. All of these characters: .,:;?!'*-_ in addition to all letters, upper and lower case, and all numbers, will be allowed in the string. If you don't want the asterisk or others, remove them from the RegExp. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> <title>None</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <script type="text/javascript"> function init(){ var nStr = "@holyinsomniac this should work now| @mike what do you think?| @Godfather| @topdog| @holyinsomniac| This is a test. Bull*** - ok, Noway"; nStr = nStr.match(/@[\w\s.,:;?!'*-]+|$/g); for (i=0; i<nStr.length; i++) { nStr[i] = nStr[i].toString().replace(/@/, " "); } nStr.length = nStr.length - 1; nStr = nStr.join(); alert(nStr); } navigator.appName == "Microsoft Internet Explorer" ? attachEvent('onload', init, false) : addEventListener('load', init, false); </script> </head> <body> </body> </html> Code (markup):
In your initial post, you wrote: NONE of my previous posts captured the @. I despise this back and forth, back and forth, with the desired result constantly changing. This is my final post in this thread. You are manipulative. That angers me. You've wasted my time. This is no good, that's no good, I want this, I want that, can you make it do this? No. Not me. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> <title>None</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <script type="text/javascript"> function init(){ var nStr = "@holyinsomniac this should work now| @mike what do you think?| @Godfather| @topdog| @holyinsomniac| This is a test. Bull*** - ok, Noway"; nStr = nStr.match(/@[\w\s.,:;?!'*-]+|$/g); nStr.length = nStr.length - 1; nStr = nStr.join().replace(/^\s+/, ""); alert(nStr); } navigator.appName == "Microsoft Internet Explorer" ? attachEvent('onload', init, false) : addEventListener('load', init, false); </script> </head> <body> </body> </html> Code (markup):
I apologize for making you feel this way due to my own confusion over this. Regardless, I appreciate your help. Thank you!