javascript string methods do not work
Hello dear friends,
i have a problem that i can not really believe.
I've got the following string from a website:
string starts at the beginning of the next line ---------------------
<a href="Plasma-DACH%20Mittwochs%20Meeting%202016-09-14.mp4">Plasma-DACH Mittwochs Meeting 2016-09-14.mp4</a> 2016-09-15 07:09 1.7G
string ends at the end of the above line ---------------------
I'm interested in the Link part, let us call the above string "var aboveString", ok and i get the link part by
var zeile = /<a href.*\/a>/.exec( aboveString );
This works fine and the variable "zeile" shows
<a href="Plasma-DACH%20Mittwochs%20Meeting%202016-09-14.mp4">Plasma-DACH Mittwochs Meeting 2016-09-14.mp4</a>
But now comes the surprise: No more string methods work on this string.
Neither zeile.replace(), zeile.search() nor zeile.split() are executed. The page just remains blank without any error message. Even zeile.indexOf( 'a' ) for example gives a result of -1.
I detected that the enclosed doubleqoutes may cause the problem. If i put single quotes around the string like this:
zeile = '\ + zeile + '\;
Then all the string methods work fine. But i have to remember them and remove them after processing the string.
So my question is:
Is there a bug in the string object that the string methods can not handle the string in the var "zeile"? Why does the RegExp exec() method not have any promlem with an even longer string? Or das JS not handle the var "zeile" as a String?
For me it looks like a string, so if JS does not handle it like a string, how could i proceed in the future? Can i rely on my experience with the variables or do i have to learn some additional behaviors of JS?
Where can i read after exactly what JS does, if it reads a line like this:
zeile.split( "#" );
If a String can really mutate to another object,, should i always for safety reasons check the type of the object, before i use their methods?
As you can see, i'm a bit confused and i hope someone can help me out it.
Thank you very much for your patience and your attention. I love you,
Richard
Chosen solution
Thank you very much for your comment, dear Seburo,
i did find the answer and will post it to close the question and help those who have the same problem.
The whole problem raised from a lack of understanding the depth of JavaScript. One has to understand, what an OBJECT in JavaScript is.
And the result of a RegExp object is a RegExp object, NOT a string!
All the problems are gone if one converts the result of a regular expressen (a RegExp object) to a string with the RegExp method "toString()". A RegExp object does not have methods like .replace(), .search() or .slice() and so on as a String object has.
So, whenever you want to treat the result of a regular expression as a string, you have to convert it like this:
var zeile = /<a href.*\/a>/.exec( aboveString );
zeile = zeile.toString();
From now on you have the variable zeile as a string and can proceed with the string methods without further disruption.
I hope this helps. Thank you very much for your attention. I love you,
Richard
Read this answer in context 👍 0All Replies (2)
Hi
Thank you for your question. This sounds very much like a programming question that is a little above our level of expertise on the Support Forum (we mainly look at Firefox product issues).
I recommend that you ask about this in Stack Overflow where I am sure one of the experts there will be able to help.
Chosen Solution
Thank you very much for your comment, dear Seburo,
i did find the answer and will post it to close the question and help those who have the same problem.
The whole problem raised from a lack of understanding the depth of JavaScript. One has to understand, what an OBJECT in JavaScript is.
And the result of a RegExp object is a RegExp object, NOT a string!
All the problems are gone if one converts the result of a regular expressen (a RegExp object) to a string with the RegExp method "toString()". A RegExp object does not have methods like .replace(), .search() or .slice() and so on as a String object has.
So, whenever you want to treat the result of a regular expression as a string, you have to convert it like this:
var zeile = /<a href.*\/a>/.exec( aboveString );
zeile = zeile.toString();
From now on you have the variable zeile as a string and can proceed with the string methods without further disruption.
I hope this helps. Thank you very much for your attention. I love you,
Richard
Modified