Err Posted November 11, 2009 Share Posted November 11, 2009 I need some help with a regex I'm trying to create for a replace function. This is how I want it set up:[0-9A-Za-z][one space][0-9A-Za-z]I'm trying to prevent spaces from being entered for the first character and prevent extra (two or more) spaces between letters. I have been trying to create my own for a while but I haven't had any luck, this is the best that I have: str.replace(/([^a-zA-Z0-9]){1}\s/g,''); The above code removes extra spaces but allows spaces for the first character. Any help with this would be great. Link to comment Share on other sites More sharing options...
Ingolme Posted November 11, 2009 Share Posted November 11, 2009 Go listing all the rules you want to follow and then translate them to the regular expression. You haven't explained yourself well so I can't be sure that the answer I give you is what you want.This expression must begin with a-zA-Z0-9, it can have spaces between the words, but must end with a character: /^([a-zA-Z0-9]+\s?)+[a-zA-Z0-9]$/g Link to comment Share on other sites More sharing options...
Err Posted November 12, 2009 Author Share Posted November 12, 2009 I tried the code, it didn't work correctly. It allowed spaces for the first character. Let me explain a little more. I want to replace beginning and extra spaces in a string.Allowed Characters: [0-9a-zA-Z\s]So something like this: beginning and extra spaces here I want to turn into this: beginning and extra spaces not here For example purposes I put together a very short example of what I'm working with. <input type="text" name="txt" onkeyup="return validate();" />function validate() { txt = document.getElementsByName('txt')[0]; txt.value = txt.value.replace(/^([a-zA-Z0-9]+\s?)+[a-zA-Z0-9]$/g, '');} Please keep in mind that I'm just getting into regular expressions and the concepts behind it seem a bit confusing for me. But if I'm starting to create expressions myself that work, then I'm progressively getting better with them. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.