user1824996
user1824996

Reputation: 259

Javascript search & replace with a new string if a match is found

I want to search an address string.

if " Southeast " is found, replace it with " SE ";

if " Southwest " is found, replace it with " SW ";

if " northeast " is found, replace it with " NE ";

if " northeast " is found, replace it with " NW ".

Here is what I did so far:

 var searchStr = [" Southeast ", " Southwest ", " Northeast ", " Northwest "];
 var replaceStr = [" SE ", " SW ", " NE ", " NW "];
 var oldAddress = $("#address").text();

 for (i=0;i<searchStr.length;i++){
     var n = oldAddress.match(/this[i]/g);
         if(n != null){
            $("#address").text(replaceStr[i]);  
         }                        
 }

It didn't do anything, am I missing something?

Upvotes: 1

Views: 164

Answers (5)

karthick
karthick

Reputation: 12176

I don't know what you are trying to do using this[i] but even if you write searchStr[i] in that place it wont work. because you are creating a string over there. Inorder for that to work you need to create an regexp object

try this code

var searchStr = [" Southeast ", " Southwest ", " Northeast ", " Northwest "];
var replaceStr = [" SE ", " SW ", " NE ", " NW "];
var oldAddress = $("#address").text();

for (var i=0;i<searchStr.length;i++){
var pattern =new RegExp(searchStr[i]);
    var n = oldAddress.match(pattern);
       if(n != null){
            $("#address").text(replaceStr[i]);  
         }                         
}

Upvotes: 0

HBP
HBP

Reputation: 16043

 var r = {
   southeast: 'SE',
   southwest: 'SW',
   northeast: 'NE',
   northwest: 'NW'
 };

 $("#address").text ($("#address").text().replace (
   RegExp (Object.keys (r).join ('|'), 'ig'), 
   function (m) { return r[m.toLowerCase ()]; }));​

See fiddle at http://jsfiddle.net/fJ7Vf/

Upvotes: 0

Siva Charan
Siva Charan

Reputation: 18064

Apart from existing answers, you can do this way too:-

 var searchStr = [" Southeast ", " Southwest ", " Northeast ", " Northwest "];
 var replaceStr = [" SE ", " SW ", " NE ", " NW "];
 var oldAddress = $("#address").text();

 for (i=0;i<searchStr.length;i++){
     oldAddress = oldAddress.replace(searchStr[i], replaceStr[i]);
 }
 $("#replaced").text(oldAddress);

Sample HTML:

<div id="address">First I went to Southeast , next to Northwest , next to Southwest and finally to Northeast </div>
<br/>
Updated content is here:
<br/>
<div id="replaced"></div>

Refer to LIVE DEMO

Upvotes: 0

VisioN
VisioN

Reputation: 145428

$("#address").text(function(i, text) {
    $.each({
        "Southeast": "SE",
        "Southwest": "SW",
        "Northeast": "NE",
        "Northwest": "NW"
    }, function(k, v) {
        var regex = new RegExp('" ' + k + ' "', "ig");
        text = text.replace(regex, '" ' + v + ' "');
    });
    return text;
});​

DEMO: http://jsfiddle.net/rKK89/1/

Upvotes: 1

Tim Joyce
Tim Joyce

Reputation: 4517

var newAddress = oldAddress
                    .replace("Southeast", "SE")
                    .replace("Southwest", "SW")
                    .replace("Northeast", "NE")
                    .replace("Northwest", "NW");
$("#address").text(newAddress);

Upvotes: 1

Related Questions