Blog coding and discussion of coding about JavaScript, PHP, CGI, general web building etc.

Saturday, January 2, 2016

Replace multiple strings at once

Replace multiple strings at once


Is there an easy equivalent to this in JavaScript?

$find = array("<", ">", "\n");  $replace = array("<", ">", "
"); $textarea = str_replace($find, $replace, $textarea);

This is using PHP's str_replace, which allows you to use an array of words to look for and replace. Can I do something like this using JavaScript / jQuery?

...  var textarea = $(this).val();    // string replace here    $("#output").html(textarea);  ...  

Answer by James Sumners for Replace multiple strings at once


One method would be:

var text = $(this).val();  text = text.replace(//g, ">");  $("#output").html(text);  

Answer by hsz for Replace multiple strings at once


text = text.replace(//g, '>').replace(/\n/g, '
');

Answer by Bryan Kyle for Replace multiple strings at once


There is no way to do this in one method call, you'll have to either chain calls together, or write a function that manually does what you need.

var s = "<>\n";  s = s.replace("<", "<");  s = s.replace(">", ">");  s = s.replace("\n", "
");

Answer by user113716 for Replace multiple strings at once


For the tags, you should be able to just set the content with .text() instead of .html().

Example: http://jsfiddle.net/Phf4u/1/

var textarea = $('textarea').val().replace(//, '\n');    $("#output").text(textarea);  

...or if you just wanted to remove the
elements, you could get rid of the .replace(), and temporarily make them DOM elements.

Example: http://jsfiddle.net/Phf4u/2/

var textarea = $('textarea').val();    textarea = $('
').html(textarea).find('br').remove().end().html(); $("#output").text(textarea);

Answer by Bob for Replace multiple strings at once


You could extend the String object with your own function that does what you need (useful if there's ever missing functionality):

String.prototype.replaceArray = function(find, replace) {    var replaceString = this;    for (var i = 0; i < find.length; i++) {      replaceString = replaceString.replace(find[i], replace[i]);    }    return replaceString;  };  

For global replace you could use regex:

String.prototype.replaceArray = function(find, replace) {    var replaceString = this;    var regex;     for (var i = 0; i < find.length; i++) {      regex = new RegExp(find[i], "g");      replaceString = replaceString.replace(regex, replace[i]);    }    return replaceString;  };  

To use the function it'd be similar to your PHP example:

var textarea = $(this).val();  var find = ["<", ">", "\n"];  var replace = ["<", ">", "
"]; textarea = textarea.replaceArray(find, replace);

Answer by Matt Rardon for Replace multiple strings at once


You might want to look into a JS library called phpJS.

It allows you to use the str_replace function similarly to how you would use it in PHP. There are also plenty more php functions "ported" over to JavaScript.

http://phpjs.org/functions/str_replace:527

Answer by Utopik for Replace multiple strings at once


It's late but you can do :

String.prototype.htmlProtect = function() {    var replace_map;      replace_map = {      '\n': '
', '<': '<', '>': '>' }; return this.replace(/[<>\n]/g, function(match) { // be sure to add every char in the pattern return replace_map[match]; }); };

and then :

var myString = "tell me a story, \nbro'";  var myNewString = myString.htmlProtect();  // tell me a story, 
bro'

Answer by user1925159 for Replace multiple strings at once


 String.prototype.replaceArray = function (find, replace) {       var replaceString = this;       for (var i = 0; i < find.length; i++) {           // global replacement           var pos = replaceString.indexOf(find[i]);           while (pos > -1) {               replaceString = replaceString.replace(find[i], replace[i]);               pos = replaceString.indexOf(find[i]);           }       }       return replaceString;   };       var textT = "Hello world,,,,, hello people.....";       var find = [".",","];       var replace = ['2', '5'];       textT = textT.replaceArray(find, replace);  

// result: Hello world55555 hello people22222

Answer by elchininet for Replace multiple strings at once


You could use the replace method of the String object with a function in the second parameter:

First Method (using a find and replace Object)

var replace = {"<" : "<", ">" : ">", "\n" : "
"}; $textarea = $textarea.replace(new RegExp("(" + Object.keys(replace).join("|") + ")", "g"), function(s){ return replace[s] });

jsfiddle

Second method (using two arrays, find and replace)

var find = ["<", ">", "\n"];  var replace = ["<", ">", "
"]; textarea = textarea.replace(new RegExp("(" + find.join("|") + ")", "g"), function(s){ return replace[find.indexOf(s)] });

jsfiddle

Desired function:

function str_replace($f, $r, $s){     return $s.replace(new RegExp("(" + $f.join("|") + ")", "g"), function(s){ return $r[$f.indexOf(s)]  });  }    $textarea = str_replace($find, $replace, $textarea);   


Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 72

0 comments:

Post a Comment

Popular Posts

Powered by Blogger.