質問

i'm trying to trim down a string before removing parts of it, but there's something off about the spaces in the string.

the string i'm trying to trim is this

        200    KristianMedK                                                                fisk                                                         {dropdownbegin}                                  Ikke valgt                        fisk                        fugl                                                dinosaur                                                kristian                                                         {dropdownend}                                                                        Ikke valgt                                                                {dropdownbegin}                                  Ikke valgt                                            KristianMedK                       Laks                       TunMedT                       lasse                           {dropdownend}                    

the string is being returned from the .text() method of an element in the tablesorter jquery library. I've already filtered out a bunch of ↵ characters from the string by

mystring = mystring.replace(/\n/g, "")

using

mystring= mystring.replace(" ","") 

made no difference either.

How can i find out where if any otoher special characters is hiding in the string messing with the javascript?

my end goal is removing the content between the {dropdownbegin} and {dropdownend} but the substring i get when getting indexof is wrong and includes some of the text after the {dropdownend}

    var index1 = text.indexOf("{dropdownbegin}");
    var index2 = text.indexOf("{dropdownend}");
    var substr = text.substr(index1, index2);
    text = text.replace(substr, "");

the above code gives the substring

{dropdownbegin} Ikke valgt fisk fugl dinosaur kristian {dropdownend} Ikke valg

for reasons i do not know.

EDIT the trim issue was solved quickly as i failed to search properly for existing questions.

役に立ちましたか?

解決 3

I will go only for your end goal:

var str = "Test with {dropdownbegin} content and stuff{dropdownend}coolness";
var filtered = str.replace(/\{dropdownbegin\}.*\{dropdownend\}/, ''); // "Test with coolness"

他のヒント

Try mystring.replace(/\s+/g, ' '); and $.trim()

As Pavel and Arun P pointed in the comment, it is a possible duplicate question.

As you can read in the documentation of the trim() function, this function removes whitespaces before and after the string but it won't touch the whitespaces in the string.

If you want to remove the whitespaces in the string too, you have to write your own function.

Here a little example:

function cleanStr(str) {
    while (str.indexOf("\t") > -1) {
        str = str.replace("\t", " ");
    }
    while (str.indexOf("  ") > -1) {
        str = str.replace("  ", " ");
    }
    return str;
}

Example of use:

cleanStr("   aasdfqwer         wer  asdaf       awer   5asadf   sdf          ");

Result:

" aasdfqwer wer asdaf awer 5asadf sdf "
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top