// s is 'aaddefgbc'
var s = 'abcdefgad'.replace(/bc/g,'ad')
Remove all occurences of the substring
// s is 'adefg'
var s = 'abcdefgbc'.replace(/bc/g,'')
Remove all occurences of character set
// remove all vowels
// s is 'bcdfgh'
var s = 'abcdefghi'.replace(/[aeoui]/g,'')
Convert char to Unicode code
// ch1 is 65
var ch1 = 'A'.charCodeAt(0);
// ch2 is 223
var ch2 = 'ß'.charCodeAt(0);
Convert Unicode code to char
// s is 'Aß'
var s = String.fromCharCode(65, 223);
Process string one character at a time
for (var i = 0; i < s.length; i++) {
// process s[i]
}
Reverse string by character
// r is 'dcba'
var r = 'abcd'.split('').reverse().join('');
Reverse string by word
// r is 'Four Thre Two One'
var r = 'One Two Thre Four'.split(/\s+/).reverse().join(' ')
Uppercase string
// s is 'ONE TWO'
var s = 'One Two'.toUpperCase()
// See also toLocaleUpperCase
Lowercase string
// s is 'one two'
var s = 'One Two'.toLowerCase()
// See also toLocaleLowerCase
Capitalize all words
String.prototype.capitalize = function() {
return this.replace(/(?:^|\s)\S/g, function(a) { return a.toUpperCase(); });
};
// s is 'One Two'
var s = 'one two'.capitalize();
URL-encode the string
// encoded is '%2Ftest%2F'
var encoded = encodeURIComponent('/test/');
URL-decode the string
// decoded is '/test/'
var decoded = decodeURIComponent('%2Ftest%2F');
Trim blanks from the both ends of a string
String.prototype.trim=function(){return this.replace(/^\s+|\s+$/g, '');};
// s is 'one two'
var s = ' one two '.trim();
Trim blanks from the beginning of a string
String.prototype.ltrim=function(){return this.replace(/^\s+/g, '');};
// s is 'one two '
var s = ' one two '.ltrim();
Trim blanks from the end of a string
String.prototype.rtrim=function(){return this.replace(/\s+$/g, '');};
// s is ' one two'
var s = ' one two '.rtrim();
Trim newlines from the ends of a string
String.prototype.chomp=function(){return this.replace(/\n+$/g, '');};
// s is 'one two'
var s = 'one two\n\n\n'.chomp();
Find position of the substring
// idx is 4
var idx = 'one two three'.indexOf('two');
Check if the string contains another string
// has is false
var has = ('one two three'.indexOf('woot') !== -1)
Check if the string starts with another string
String.prototype.startsWith=function(str){return (this.indexOf(str) === 0);};
// b is true
var b = 'one two'.startsWith('one');
// b is false
b = 'one two'.startsWith('two');
Check if the string ends with another string
String.prototype.endsWith=function(str){return this.indexOf(str, this.length - str.length) !== -1;};
// b is false
var b = 'one two'.endsWith('one');
// b is true
b = 'one two'.endsWith('two');
Convert the string to integer
// i is 1000
var i = parseInt("1000", 10);
// h is 255
var h = parseInt("ff", 16);
Convert the string to float
var f = parseFloat("10.01");
Convert float value to string
var f = 1.25;
// s is '1.25'
var s = f.toString();
Convert integer value to string
var n = 99;
// s is '99'
var s = n.toString();
Enumerate all words in the string
var line = 'one two three';
var words = line.split(/\s+/);
for (var i in words) {
console.log(words[i]);
}