TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Javascript, AJAX, E4X (http://www.talkphp.com/javascript-ajax-e4x/)
-   -   RegEx (http://www.talkphp.com/javascript-ajax-e4x/4792-regex.html)

buildakicker 07-29-2009 12:14 AM

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!

tony 07-29-2009 02:46 AM

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.

Salathe 07-29-2009 09:57 AM

Quote:

Originally Posted by buildakicker (Post 27382)
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;
   
}

buildakicker 07-29-2009 02:38 PM

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!

tony 07-29-2009 03:24 PM

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.

buildakicker 07-29-2009 08:04 PM

Great. I was thinking that \W need the escape. Thanks for the post!

tony 07-30-2009 01:30 AM

no problem


All times are GMT. The time now is 10:30 PM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0