0

I have a string such as:

http://www.google.com/#anchor-name

and I want to be able to get

http://www.google.com/

and

anchor-name

into variables? How do I do it geniuses of the stackoverflow community?

NotaGuruAtAll
  • 503
  • 7
  • 19
  • 2
    possible duplicate of [How do I split a string, breaking at a particular character?](http://stackoverflow.com/questions/96428/how-do-i-split-a-string-breaking-at-a-particular-character) – Bojangles Aug 19 '14 at 14:34
  • @Bojangles - However, splitting an URL into it's parts is something different. – adeneo Aug 19 '14 at 14:39
  • @adeneo True, and this is somewhat of an XY Problem question, but if all the OP wants to do is split on a `#`, the dupe I linked is correct. If they want to parse the URL further then a parsing library is the best way to go. Either way this question is still a dupe – Bojangles Aug 19 '14 at 14:52

2 Answers2

0

In your situation

var str = "http://www.google.com/#anchor-name";
var parts = str.split('#');
console.log(parts)//["http://www.google.com/","anchor-name"]
Mritunjay
  • 25,338
  • 7
  • 55
  • 68
  • This has been answered before and can be found in countless Google search result and many duplicate StackOverflow questions. Please don't waste your effort by answering this question – Bojangles Aug 19 '14 at 14:35
0

Try

var parts = str.split('#');
var left = parts[0], right = parts[1];
Yury Tarabanko
  • 44,270
  • 9
  • 84
  • 98
  • 1
    This has been answered before and can be found in countless Google search result and many duplicate StackOverflow questions. Please don't waste your effort by answering this question – Bojangles Aug 19 '14 at 14:35