/    Sign up×
Community /Pin to ProfileBookmark

problems with .replace function

Hey all,

Anyone have any ideas what i have done wrong in the code below? It works in all browsers but not Firefox.

Firefox keeps saying the following,

invalid regular expression flag w

The code….

// check for emails, then link emails

var eMail = UserMessage.match(/([a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+.[a-zA-Z0-9._-]+)/gi);
var eMailLink = ‘<a href=”mailto:’+eMail+'”>’+eMail+'</a>’;

UserMessage = UserMessage.replace(eMail,eMailLink,UserMessage);

// check for urls, then link urls

var uRL = UserMessage.match(/([[url]http://._-]+[/url][a-zA-Z0-9._-]+.[a-zA-Z0-9._-]+)/gi);
var uRLLink = ‘<a href=”‘+uRL+'” target=”_
blank”>’+uRL+'</a>’;

UserMessage = UserMessage.replace(uRL,uRLLink,UserMessage);

First to answer gets a beer, Cheers! :p

PS. If i really messed my code up, don’t blame me, im still learning!!

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@voidvectorNov 22.2008 — String.replace() only takes 2 parameters, and it can be regexp. So I have shorten your code a bit.

Your URL regex was incorrect, so I just replaced it with a simple one. You may be want to get a more robust URL regex, since I just made this one up.

Assuming this forum didn't mess up your regex code. Putting HTTP in brackets would match characters H, T, P, not the string.

[code=html]
<html>
<head>
<script>
var UserMessage = "My email is [email protected]. My website is http://www.yahoo.com/made-up/~address/?query=d&query2=1 .";
UserMessage = UserMessage.replace(/([w.-]+@[w.-]+.[w]+)/gi, '<a href="mailto:$1">$1</a>');
UserMessage = UserMessage.replace(/(http[s]?://[S]+)/gi, '<a href="$1" target="_blank">$1</a>');
document.write(UserMessage);
</script>
</head>
<body>
</body>
</html>
[/code]
Copy linkTweet thisAlerts:
@NutauthorNov 22.2008 — Cheers VoidVector,

Awesome fix, it works perfectly!

From what you've said, looks like i've confused PHP with Javascript. :p
×

Success!

Help @Nut spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...