-4

I am familiar with the "replace" method in javascript is not enough. Possibly is it for my project is also a better way, then I would be glad if you also tells me this.

I would need the following:

I have a string with the following content:

var address = 'Beethovenstrasse 1, 97080 Würzburg';

This should be converted to:

var addressNew = '97080/W%C3%9CRZBURG/Beethovenstrasse/1';

And another string

var address = 'Hitzelsbergstr. 84, 83233 Bernau';

This should be converted to:

var addressNew = '83233%20Bernau,%20Hitzelsbergstr.%2084';

To explain why I need this: I need to convert the addresses are in the form of the string "address" in a database. The new string "addressNew" are then part of a link, which starts on mobile devices or TOMTOM navigation NAVIGON and the address to the destination passes by.

Ever Thanks for your help

Thomas
  • 3
  • 1
  • Why is Wurzburg capital in first while Bernau isn't? Also what's your basic try? – Amit Joki Jan 23 '15 at 08:57
  • Yes, you can do this with `replace`. You'd use capture groups. You may or may not need to use a function as the second argument (why does 97080 get `/W` after it, but 83233 gets `%20`?). Give it a go. Read the docs. – T.J. Crowder Jan 23 '15 at 08:57
  • 3
    The two addressNew seem to be in different format. Is that correct? – A. Rama Jan 23 '15 at 08:58
  • What did you try so far? Also, are you certain that addresses will always comply to the pattern given? – mritz_p Jan 23 '15 at 08:58
  • Why do you need to convert them in such a way? do you need to parse them somewhere or what? Also, they are different. – briosheje Jan 23 '15 at 08:59
  • @Amit Joki: The app "navigon" does not require this information in this way, the app "tomtom". – Thomas Jan 23 '15 at 09:03
  • @ T.J. Crowder: The app "navigon" does not require this information in this way, the app "tomtom". The strings are needed different. I have expressed something wrong with the variables. Let's say that the first string is "var addressNewNavigon" and the second "var addressNewTomtom". – Thomas Jan 23 '15 at 09:06

2 Answers2

0

A simple solution:

var address = 'Beethovenstrasse 1, 97080 Würzburg';
var parts = address.split(', ');
var newString = parts[1] + '/' + parts[0];
console.log(encodeURIComponent(newString));

This solution is very specific and does not handle other formats, but you should get some ideas.

ntalbs
  • 28,700
  • 8
  • 66
  • 83
andeersg
  • 1,505
  • 2
  • 13
  • 24
-1

You can split the string based on spaces and than you can adjeust the order and repalce characters with %20

Joris Janssen
  • 64
  • 1
  • 10
  • This is not easy, the addresse may also be in this form: var address = 'Dr Hans-Kapfinger-Strasse 1, 94057 Bad Kötzing'; or var address = 'Bahnhofstrasse 1, 80000 Munich'; – Thomas Jan 23 '15 at 09:11
  • but it will always be in the form (street) (street number) (code)(city) ? – Joris Janssen Jan 23 '15 at 12:48