0

I'm working with morse code and I was wondering if it's possible to replace a space with a forward slash using jQuery?

Thanks

P. Smythe
  • 5
  • 2
  • Can you be more specific and provide [example code](http://stackoverflow.com/help/mcve) or a [jsfiddle](https://jsfiddle.net/)? – urban Jan 04 '16 at 19:57
  • 1
    Here's a jsfiddle https://jsfiddle.net/a1L8racn/ – P. Smythe Jan 04 '16 at 19:59
  • If I understand correctly you need to replace _all_ spaces with `/`. This is done with RegEx as `str.replace(/ /g, '/')` For more information see [here](http://stackoverflow.com/questions/1144783/replacing-all-occurrences-of-a-string-in-javascript). Note that in your example you can also use `array.join('/')` – urban Jan 04 '16 at 20:05
  • I can't seem to get it working. It's an issue with the split and join that I can't figure out myself, I'm too dumb. Argh. – P. Smythe Jan 04 '16 at 20:11
  • Solved by adding an entry to my morse: `" ": " / "` – P. Smythe Jan 04 '16 at 21:01

2 Answers2

2

jQuery is wholly the wrong tool here. jQuery is a package built on top of JavaScript for manipulation of the DOM. You want string manipulation, which is just methods as part of the String prototype inside JavaScript:

var someString = "My String is Cool";
someString = someString.replace(' ','/');
alert(someString);

For this case, we're simply using one for one replacement (the character space is replaced by the character slash). More complex replacements may utilize regex or Regular Expressions, which again, is not jQuery.

0

If I interpret your question correctly, you want to know how to replace parts of a string (in your cases, spaces) with another string (a forward slash)? In that case, you might want to give String.replace() a try.

Here's a few examples: How to replace all occurrences of a string in JavaScript?

Community
  • 1
  • 1
TomTasche
  • 5,448
  • 7
  • 41
  • 67