TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 07-29-2009, 12:14 AM   #1 (permalink)
The Acquainted
 
buildakicker's Avatar
 
Join Date: Jan 2008
Posts: 119
Thanks: 21
buildakicker is on a distinguished road
Smile RegEx

Hi all,

My other post seemed to be a little much?! Maybe this one will be easier...

I am trying to strip an input of any special characters using regExpressions, but am not quite understaning how to get rid of anything but letters and numbers. Isn't there a replace /(!@#%$*())/ all that with "" expression?

Here is what I have so far:

Code:
function updatelink()
{
	var atitle = document.getElementById("articleTitle").value;
	var result = atitle.replace(/(\s| | )+/gi, "-"); //replace spaces
	var clean = result.replace(/'/,""); //replace punctuation
	var postlink = document.getElementById("postlink");
	postlink.firstChild.nodeValue = clean;
	
}
How do I add more than the ' inside the var clean replace?

Thanks!
__________________
SkiLeases.com
buildakicker is offline  
Reply With Quote
Old 07-29-2009, 02:46 AM   #2 (permalink)
The Addict
 
tony's Avatar
 
Join Date: Aug 2008
Posts: 336
Thanks: 8
tony is on a distinguished road
Default

This regex could help
Code:
[^A-Za-z0-9]+
it matches anything that is not alphanumeric.
or also the W that matches all alphanumeric, but the W treats the underscore like an alphanumeric. so this
Code:
(/W)+
is the same as
Code:
[^A-Za-z0-9_]+
P.S. I haven't test it.
tony is offline  
Reply With Quote
Old 07-29-2009, 09:57 AM   #3 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

Quote:
Originally Posted by buildakicker View Post
how to get rid of anything but letters and numbers.
I assume you also want to keep hyphens since they represent where whitespace is in the article title. Here's an amended version of your function, the two regular expressions have been changed to better suit what you're looking for.

JavaScript Code:
function updatelink()
{
    var title = document.getElementById("articleTitle").value;

    // 1. Replace whitespace with hyphen character
    var clean = title.replace(/\s+/g, "-");
    // 2. Strip non-alpnanumeric-or-hyphen characters
    clean = clean.replace(/[^A-Z0-9-]/gi, "");

    var postlink = document.getElementById("postlink");
    postlink.firstChild.nodeValue = clean;
   
}
Salathe is offline  
Reply With Quote
The Following User Says Thank You to Salathe For This Useful Post:
buildakicker (07-29-2009)
Old 07-29-2009, 02:38 PM   #4 (permalink)
The Acquainted
 
buildakicker's Avatar
 
Join Date: Jan 2008
Posts: 119
Thanks: 21
buildakicker is on a distinguished road
Default

Hey Ya! Thanks you guys.

In a string link this:

Code:
var clean = title.replace(/\s+/g, "-");
How do you put this: (/W)+

Like so:
Code:
var clean = title.replace(/(/W)+/g, "-");
? It doesn't like it... Do I need to escape the + ?

Confusing stuff, but essential!
__________________
SkiLeases.com
buildakicker is offline  
Reply With Quote
Old 07-29-2009, 03:24 PM   #5 (permalink)
The Addict
 
tony's Avatar
 
Join Date: Aug 2008
Posts: 336
Thanks: 8
tony is on a distinguished road
Default

my bad, it is \W
so something like this would do
javascript Code:
var title = document.getElementById("articleTitle").innerHTML,
    postlink = document.getElementById("postlink"),
    postlinkcontent = postlink.innerHTML,
    clean = title.replace(/\s+/g, "_");
clean = clean.replace(/(\W)/gi, "");
postlink.innerHTML = clean + postlinkcontent;

it is better to use innerHTML than value, I think that is only for form elements, I am not sure.
tony is offline  
Reply With Quote
The Following User Says Thank You to tony For This Useful Post:
buildakicker (07-29-2009)
Old 07-29-2009, 08:04 PM   #6 (permalink)
The Acquainted
 
buildakicker's Avatar
 
Join Date: Jan 2008
Posts: 119
Thanks: 21
buildakicker is on a distinguished road
Default

Great. I was thinking that \W need the escape. Thanks for the post!
__________________
SkiLeases.com
buildakicker is offline  
Reply With Quote
Old 07-30-2009, 01:30 AM   #7 (permalink)
The Addict
 
tony's Avatar
 
Join Date: Aug 2008
Posts: 336
Thanks: 8
tony is on a distinguished road
Default

no problem
tony is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
RegEx ReSpawN Advanced PHP Programming 10 05-10-2008 01:01 PM
Evaluate my regex pleeze? Aaron General 8 05-05-2008 01:24 AM
Is this a propert URL regex? Aaron Absolute Beginners 2 04-16-2008 05:18 PM
RegEx xenon General 6 12-12-2007 10:38 AM
Need Help with RegEx webosb Absolute Beginners 6 12-09-2007 04:17 PM


All times are GMT. The time now is 06:57 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design