0

In a variable I have a string value Agriculture & Development. I want to remove Whitespaces and character '&' from the string Agriculture & Development so that it looks like Agriculture_Development using jquery or javascript.

Ozair Kafray
  • 13,351
  • 8
  • 59
  • 84
Asim Naseer
  • 143
  • 1
  • 3
  • 11
  • 1
    So far so good. Apparently you don't have a question here, so just go ahead and do it. – lanzz Jul 06 '12 at 06:58
  • Please Check this below link First google it before ask question http://stackoverflow.com/questions/2145988/string-replace-in-jquery – muthu Jul 06 '12 at 07:01

2 Answers2

4

No need for jquery here, plain js will do:

var nwstr = 'Agriculture & Development'
                .replace(/\s+/g,'')
                .replace(/\&/,'_');
//=> Agriculture_Development

Now go and figure things out yourself

KooiInc
  • 119,216
  • 31
  • 141
  • 177
  • 3
    Or `'Agriculture & Development'.replace(/[\s&]+/g,'_')` to simply replace all groups of space and/or `&` with a single `_`. – Supr Jul 06 '12 at 07:16
1
var str = "Agriculture & Development"; 
// replace all the white space and the &
str.replace(/\s+/g, '').replace(/\&/,'_');
TRR
  • 1,637
  • 2
  • 26
  • 43