brodiemigas
brodiemigas

Reputation: 97

Javascript Regex Whitespace Replacement

I am trying to get the value of an <select></select> tag as set from the selected <option> value and replace any whitespace in the value with a dash. at the moment I am just then console.log() the value for testing but for some reason instead of selecting whitespace it is selecting and replacing lower case "s". The code I am using is: <select onchange='var cat = this.value; var modi = cat.replace(/\s/g , "-"); console.log(modi);'>

Example: for example assume the this.value is equal to "test value"

Output: te-t value Expected Output: test-value

Upvotes: 0

Views: 73

Answers (4)

Kumar
Kumar

Reputation: 280

Try this

function myFunction() {
        var str = "Is this all there is?";
        var patt1 = / /g;
        var result = str.replace(patt1,"-");            
    }

Upvotes: 1

Varun Sreedharan
Varun Sreedharan

Reputation: 577

Try this method

var reg = new RegExp(" ","g");

Upvotes: 1

Kalu Singh Rao
Kalu Singh Rao

Reputation: 1697

var str = "test value";
str = str.replace(/ /g,"-");
alert(str);

Upvotes: 1

bhanu avinash
bhanu avinash

Reputation: 482

Try this

cat.split(" ").join("-");

Upvotes: 4

Related Questions