admin管理员组文章数量:1352828
I have written a following code to get just the file name without extension and path.I m running it in 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 code works if i replace'\' by another character ex.('p'); i m initializing var str just for ex but in my application it is not always fixed.As i m new to Javascript can any body plz tell me what is the problem?n how to solve it?
I have written a following code to get just the file name without extension and path.I m running it in 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 code works if i replace'\' by another character ex.('p'); i m initializing var str just for ex but in my application it is not always fixed.As i m new to Javascript can any body plz tell me what is the problem?n how to solve it?
Share Improve this question edited Oct 27, 2009 at 11:30 falstro 35.7k10 gold badges76 silver badges88 bronze badges asked Oct 27, 2009 at 10:14 user188944user188944 2- Request an editor to edit the title to reflect the escaping issue. Mad props for fixing the text body too (spacing, caps, etc.). – JXG Commented Oct 27, 2009 at 10:37
- Don't forget to accept one answer (the first correct or the most useful for you). – PhiLho Commented Oct 27, 2009 at 13:45
7 Answers
Reset to default 5You need to escape your backslash character. Use the following:
var beg=str.lastIndexOf("\\");
EDIT: Yes, it will give a -1 unless you escape the backslashes in your original string as well :)
Use this:
var str=new String("C:\\Documents and Settings\\prajakta\\Desktop\\substr.html");
Backslash is the Javascript escape character - this means that characters following a backslash refer to special characters. Thus, in your original string, \prajakta would be interpreted as '\p' + 'rajakta' where '\p' has a very different meaning. Thus, you need to use '\\
' everywhere in every string.
"\"
is the escape character, try with "\\"
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 testing I saw that you have to escape the backslashes in the test string also!
You need to escape "\" in most languages, because \ is an escape-sequence. Javascript is probably the same.
Try searching for "\\"
(without the space) instead. Also, replace "C:\Docume..."
with "C:\\Documents..."
for the same reason
Try
lastIndexOf("\\")
Before putting your string into the variable you should escape(String)
it.
Backslash is generally the escape character, so you'll have to enter it twice whenever it occurs in a string, i.e. your
var str=new String("C:\Documents and Settings\prajakta\Desktop\substr.html");
should read
var str=new String("C:\\Documents and Settings\\prajakta\\Desktop\\substr.html");
The thing is, \D
is interpreted by the javascript engine, and will be replaced by the appropriate special character (in this case, I don't believe there's a special character \D
(or 'p' or 's'), so it'll be replaced by simply D
, and your string contents will be
"C:Documents and SettingsprajaktaDesktopsubstr.html"
Go ahead, check it with a simple
if (str == "C:Documents and SettingsprajaktaDesktopsubstr.html") alert("Doh! :)");
it should give you the alert.
Also, your
var beg=str.lastIndexOf("\");
should read
var beg=str.lastIndexOf("\\");
HTH.
in addition to what's already said, "new String" makes no sense here
var str="C:\\Documents and Settings\\prajakta\\Desktop\\substr.html";
and yes, regexps are the way to go
fileName = str.match(/([^\\.]+)\.\w+$/)[1]
本文标签: Searching for 3939 using javascriptStack Overflow
版权声明:本文标题:Searching for '' using javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743874078a2554006.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论