Javascript stripping [ and ] from strings

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
mikebr
Forum Contributor
Posts: 243
Joined: Sat Sep 28, 2002 7:05 am

Javascript stripping [ and ] from strings

Post by mikebr »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


I am trying to do some work in an application action that is written based on the javascript language but seem having a problem trying to strip the following chars '[' and ']' from a string.

Basically a string such as one of the following 4 examples will be passed:

Code: Select all

f[sometext]
f['sometext']
$f[sometext]
$f['sometext']
The different methods I have tried are as follows:

Code: Select all

mySelectName = mySelectName.replace( new RegExp( "'", "gi" ), "" );
mySelectName = mySelectName.replace( new RegExp( "f[", "gi" ), "" );
mySelectName = mySelectName.replace( new RegExp( "]", "gi" ), "" );

Code: Select all

mySelectName = mySelectName.replace(/f[/gi, "")
mySelectName = mySelectName.replace(/]/gi, "")

Code: Select all

mySelectName = mySelectName.replace("'", "")
mySelectName = mySelectName.replace("f//[", "")
mySelectName = mySelectName.replace("//]", "")
But keep getting unterminated character class errors for ]

Anyone any ideas on how to strip these chars without getting an error?

Thanks


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

[quote="[url=http://forums.devnetwork.net/viewtopic.php?t=30037]Forum Rules[/url] Section 1.1"][b]1.[/b] Select the correct board for your query. Take some time to read the guidelines in the sticky topic.[/quote]
mikebr
Forum Contributor
Posts: 243
Joined: Sat Sep 28, 2002 7:05 am

Post by mikebr »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


I think I have got around this problem using indexof(), might be a bit ugly but it seems to work:

[syntax="javascript"]function removeCSB(s) {
    // $f[myname]
    alert (s);
    s = s.replace("'","");
    var srStart = s.indexOf("[");
    var srEnd = s.indexOf("]");
    
    if (srStart>=0) {
        srStart = (srStart+1);
    }
     return s.substr(srStart,sSelect);
}

feyd | Please use[/syntax]

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Post by Jonah Bron »

This regex works. Tested.

Code: Select all

mySelectName = mySelectName.replace(/(\$?)f\[(['"]?)(.*?)(['"]?)\]/, "$3");
nickvd
DevNet Resident
Posts: 1027
Joined: Thu Mar 10, 2005 5:27 pm
Location: Southern Ontario
Contact:

Post by nickvd »

The error you are getting stems from the fact that the square brackets ([ ]) are used to define character sets in a regular expression. You need to escape them if you want to use them literally in the regex...

Such as

Code: Select all

mySelectName = mySelectName.replace(/\]/gi, "") 
Note the escaped bracket (\[) as opposed to ([)
Post Reply