Home › Forums › JavaScript › Maxlength textarea
- This topic is empty.
-
AuthorPosts
-
May 5, 2009 at 9:58 am #24793OskarParticipant
Hi,
I need a script to manage maxlength in a textarea. I’m currently using one from anon-design, which is working just fine.
There’s one big problem tho. It parses
Code:[url]test[/url]as 15 chars instead of just 4, as I need it to. Do you know of any other maxlength script or just a way to fix this?
Thanks,
OskarMay 5, 2009 at 11:06 am #57160akeenlabsParticipantIt sounds like you’ll have to modify the script to first strip out any accepted tags and then count what remains. Assuming you want to support a subset of the BBCode tags, you could do something like this:
Code:var stripBBCode = function(value){var tags = new Array(‘b’, ‘i’, ‘u’, ‘url’, ‘quote’);
var result = value;for( var index in tags){
var oRegExp = new RegExp(‘\[‘ + tags[index] + ‘](.*?)\[/’ + tags[index] + ‘]’);
result = result.replace(oRegExp, “$1”);
oRegExp = null;
}return result;
}I don’t guarantee this will work perfectly in all circumstances, but I think you can see where I’m going and expand it to suite your needs.
May 6, 2009 at 9:07 am #57197OskarParticipantJustin, that worked wonders! Thank you very very much :)
May 11, 2009 at 8:35 am #57403OskarParticipantI’ve found a little glitch, the expression matches
Code:[tag]four[/tag]just fine (as 4 chars), but
Code:[url=someurlhere]test[url]fails.
I’ve tried some things but none are working for me. Is there a simple fix? As most others I’m not too fond of the syntax =) This is the current expression:
Code:new RegExp(‘\[‘ + tags[index] + ‘](.*?)\[/’ + tags[index] + ‘]’);May 11, 2009 at 12:50 pm #57407StackMembergive this a shot.
Code:new RegExp(‘\[‘ + tags[index] + ‘](.*?)\[/’ + tags[index] + ‘]||[‘ + tags[index] + ‘=(.*?)](.*?)\[/’ + tags[index] + ‘]’);May 11, 2009 at 8:48 pm #57409Chris CoyierKeymasterYou guys are friggin geniuses. I really need to learn me some regular expressions.
May 14, 2009 at 11:23 am #57507akeenlabsParticipant"Stack" wrote:give this a shot.Code:new RegExp(‘\[‘ + tags[index] + ‘](.*?)\[/’ + tags[index] + ‘]||[‘ + tags[index] + ‘=(.*?)](.*?)\[/’ + tags[index] + ‘]’);This doesn’t work for about 10 different reasons. The most important part is that you’ve split the expression up into two different sections that repeat basically the same expression which renders the replace part of the code unusable; you’ve also introduced new capture groups that are unused so they provide a big performance penalty.
I’ve modified the original expression to make an "=something" optional:
Code:new RegExp(‘\[‘ + tags[index] + ‘(?:=.+?)?](.*?)\[/’ + tags[index] + ‘]’);Notice that I made the "=something" group non-capturing. This way, we still only have 1 capturing group to the replace code works as is. I’ve also added the requirement that something must come after the equals sign, otherwise this would match: "something"; if you don’t want this requirement, change the + to a *.
Let me know if there are any other glitches and I’ll be glad to give it another look.
-
AuthorPosts
- The forum ‘JavaScript’ is closed to new topics and replies.