0

    var storeName = "St. Bob's Store";
    var storeId = storeName.replace(/./g,"").replace(/\s/g, '').replace(/'/g,"")
    $('#storeName').html(storeName)
    $('#storeId').html("(" + storeId + ")")
    
    console.log("Updating " + storeName + "(" + storeId + ")");
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="storeName">Loading</div>
<div id="storeId">loading</div>

What am I doing wrong with storeId? It's empty.

4
  • 1
    If your intent is to replace the dot in storeName, you need to escape it in the regex. A dot means "any single character". Commented Oct 22, 2015 at 20:50
  • 1
    Keep in mind that expert's exchange != expert sex change. Same storeId though! Commented Oct 22, 2015 at 21:59
  • lol thanks, but this won't be a problem in my case :) Commented Oct 26, 2015 at 13:35
  • Although I do have a customer named Pen Island... :) Commented Oct 26, 2015 at 13:36

2 Answers 2

2

If you want to match "dot" char, you have to escape it, like this:

var storeId = storeName.replace(/\./g,"").replace(/\s/g, '').replace(/'/g,"");

Here's a fiddle: https://jsfiddle.net/e63bq01L/

If not escaped, the dot matches all characters in a string.

Sign up to request clarification or add additional context in comments.

Comments

2

You have to escape the dot character:

storeName.replace(/\./g,"").replace(/\s/g, '').replace(/'/g,"")

Otherwise, you will replace everything.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.