Skip to content

Instantly share code, notes, and snippets.

@JavaScript-Packer
Created January 11, 2016 17:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JavaScript-Packer/3ab29f9fc3f92e032634 to your computer and use it in GitHub Desktop.
Save JavaScript-Packer/3ab29f9fc3f92e032634 to your computer and use it in GitHub Desktop.
JavaScript function to swap 2 sets of strings with each other. Switch double quotes with single quotes for example. www.WHAK.ca
function swap_str(e, r, t) {//function to swap 2 sets of strings with each other
return e = e.split(r).join("WHAK_a_SWAP"), e = e.split(t).join("WHAK_b_SWAP"), e = e.split("WHAK_a_SWAP").join(t),
e = e.split("WHAK_b_SWAP").join(r);
}
//test 1
var str = 'this is "test" of a \'test\' of swapping strings';
var manipulated = swap_str(str,"'",'"');
document.writeln(manipulated)
//test 2
manipulated = swap_str(manipulated,"'",'"');
document.writeln('<hr>'+manipulated)
//test 3
manipulated = swap_str(manipulated,"test","of");
document.writeln('<hr>'+manipulated)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment