Page 1 of 1

Javascript stripping [ and ] from strings

Posted: Sat Dec 15, 2007 4:37 am
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]

Posted: Sat Dec 15, 2007 7:25 am
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]

Posted: Thu Dec 20, 2007 5:31 pm
by Jonah Bron
This regex works. Tested.

Code: Select all

mySelectName = mySelectName.replace(/(\$?)f\[(['"]?)(.*?)(['"]?)\]/, "$3");

Posted: Thu Dec 20, 2007 6:00 pm
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 ([)