简体   繁体   English

删除JavaScript字符串中的某些字符?

[英]Remove certain characters in JavaScript string?

Let's say I have a string: http://www.foo.com/#bar 假设我有一个字符串: http://www.foo.com/#bar : http://www.foo.com/#bar

How would I go about removing everything before, and including, # so I'm only left with bar ? 我将如何删除所有内容,包括#所以我只剩下bar

您可以使用split:

var myBar = "http://www.foo.com/#bar".split("#")[1];

IF the location was LOADED you can do: 如果位置已加载,则可以执行以下操作:

window.location.hash.slice(1)

window.location is an object with a bunch of useful stuff in it like the current security mode (http/s, domain, subdomain, etc.). window.location是一个对象,其中包含许多有用的内容,例如当前的安全模式(http / s,域,子域等)。 The hash property contains everything starting with # . hash属性包含以#开头的所有内容。 Slice is a method of both String and Array , allowing your to start at X and optionally end at Y. (so slice(1) is the same as "start after first and go until the end). Slice是StringArray一种方法,允许您从X开始并可以选择在Y结束。(因此slice(1)等同于“先开始,然后结束”。

However, the hash property will not show what was typed into the location bar before a pageload. 但是,hash属性不会显示页面加载之前在位置栏中键入的内容。 The location property of window refers to load location of the document, not the arbitrary things typed into the browser bar without the user submitting them. window的location属性是指文档的加载位置,而不是在用户不提交的情况下在浏览器栏中键入的任意内容。

If you just want to get the hash from the URL location.hash will return it.( It is possible to have a URL with more then one #) 如果您只想从URL位置获取哈希值,则hash将返回该哈希值(URL可能包含多个#)
http://www.w3schools.com/jsref/prop_loc_hash.asp http://www.w3schools.com/jsref/prop_loc_hash.asp

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM