Searching for '\' with javascript

I wrote the following code to get just the filename without extension and path. I am running it in a browser.

<script type="text/javascript">
var str=new String("C:\Documents and Settings\prajakta\Desktop\substr.html");
document.write(str);
var beg=str.lastIndexOf("\");// **HERE IS THE PROBLEM IT DOESNT GIVE ME THE INDEX OF '\'**
 alert(beg);
var end=str.lastIndexOf (".");
 alert(end);
document.write("<br>"+str.slice(beg+1,end));
</script> 

      

but the same code works if I replace '\' with another ex character. ('p'); im initializing var str for ex only, but in my application it is not always fixed. How can I add any body plz to Javascript, tell me what is the problem? n how to solve it?

+2


source to share


8 answers


You need to escape your backslash character. Use the following:

var beg=str.lastIndexOf("\\");

      

EDIT: Yes, it will give -1 unless you avoid backslashes in the original string :)



Use this:

var str=new String("C:\\Documents and Settings\\prajakta\\Desktop\\substr.html");

      

The backslash is a Javascript escape character - this means that characters following the backslash are special characters. So in your original line \ prajakta will be interpreted as '\ p' + 'rajakta', where '\ p' has a very different meaning. Thus, you need to use ' \\

' everywhere on every line.

+4


source


Read this article about javascript string escaping using backslash \



+4


source


"\"

is an escape character, try "\\"

Anyway, I would do it with regexes, just because I like them :)

var str=new String("C:\\Documents and Settings\\prajakta\\Desktop\\substr.html");
document.write(str);
document.write("<br>"+str.replace(/^.*\\/,"").replace(/\..*?$/,""));

      

Oh, and in testing I've seen that you also need to avoid backslashes in your test string!

+1


source


You need to escape "\" in most languages ​​because \ is an escape sequence. Perhaps Javascript is the same.

Try searching "\\"

(no space). Also, replace "C:\Docume..."

with "C:\\Documents..."

for the same reason

0


source


Try

lastIndexOf("\\")

      

0


source


The backslash is usually an escape character, so you'll have to type it twice when it happens on a string, i.e. your

var str=new String("C:\Documents and Settings\prajakta\Desktop\substr.html");

      

must read

var str=new String("C:\\Documents and Settings\\prajakta\\Desktop\\substr.html");

      

The point is that it is \D

interpreted by the javascript engine and will be replaced with the corresponding special character (in this case I don't believe there is a special character \D

(or 'p' or 's'), so it will be replaced simply D

and your string content will be

"C:Documents and SettingsprajaktaDesktopsubstr.html"

      

Go ahead, check it out with a simple

if (str == "C:Documents and SettingsprajaktaDesktopsubstr.html") alert("Doh! :)");

      

he should give you a warning.

Also, your

var beg=str.lastIndexOf("\");

      

must read

var beg=str.lastIndexOf("\\");

      

NTN.

0


source


Before you insert a string into a variable, you must have escape(String)

it.

0


source


in addition to what has already been said, "newline" is meaningless here.

var str="C:\\Documents and Settings\\prajakta\\Desktop\\substr.html";

      

and yes, regex is the way to go

fileName = str.match(/([^\\.]+)\.\w+$/)[1]

      

0


source







All Articles