how to sort string aray in ts


// TypeScript
const value = ['2020', '2017', '2019', '2018']
value.sort((a,b) => a < b ? -1 : a > b ? 1 : 0) // Ascending
value.sort((a,b) => a < b ? 1 : a > b ? -1 : 0) // descending

// javaScript
const value = ['2020', '2017', '2019', '2018']
Array.from(value).sort((a,b) => a-b) // Ascending
Array.from(value).sort((a,b) => b-a) // descending

www.codegrepper.com
www.stackoverflow.com

how to split a string in javascript


//split into array of strings.
var str = "Well, how, are , we , doing, today";
var res = str.split(",");

strName.split(); // My code

// bad example https://stackoverflow.com/questions/6484670/how-do-i-split-a-string-into-an-array-of-characters/38901550#38901550

var arr = foo.split('');
console.log(arr); // ["s", "o", "m", "e", "s", "t", "r", "i", "n", "g"]

var str = 'It iS a 5r&e@@t Day.'
var array = str.split(" ");
print(array);

var str = 'It iS a 5r&e@@t Day.'
var array = str.split(" ",2);
print(array);

var str = "How are you doing today?";

var res = str.split(" ");

// Split string into an array of strings
const path = "/usr/home/chucknorris"
let result = path.split("/")
console.log(result)
// result
// ["", "usr", "home", "chucknorris"]
let username = result[3]
console.log(username)
// username
// "chucknorris"

www.codegrepper.com
www.stackoverflow.com

how to split string from position in javascript


<script> 
// JavaScript Program to illustrate split() function 
  
function func() { 
    //Original string 
    var str = 'Geeks for Geeks'
    var array = str.split("for"); 
    document.write(array); 

  
func(); 
</script> 

www.codegrepper.com
www.stackoverflow.com

how to replace all the string in javascript when the string is javascript variable


const p = 'dog dog cat rat';

const regex = /dog/gi;

console.log(p.replace(regex, 'cow'));
//if pattern is regular expression all matches will be replaced
//output: "cow cow cat rat"

function name(str,replaceWhat,replaceTo){
replaceWhat = replaceWhat.replace(/[-/\^$*+?.()|[]{}]/g, '\$&');
var re = new RegExp(replaceWhat, 'g');
return str.replace(re,replaceTo);
}

function name(str,replaceWhat,replaceTo){
var re = new RegExp(replaceWhat, 'g');
return str.replace(re,replaceTo);
}

www.codegrepper.com
www.stackoverflow.com

how to replace characters in a string javascript


str.replace(/hello/g, 'hi');
// the g is to show it's a global change, not one-time change.

str.replace(new RegExp('hello', 'g'), 'hi');

const search = 'duck'
const replaceWith = 'goose';

const result = 'duck duck go'.replaceAll(search, replaceWith);

result; // => 'goose goose go'

const p = 'Its going to rain today and its going to rain tomorrow';

// Example of replacing all occurrances

// g = global search, which searches all instances
// i = case insensitive, which will match 'a' with 'A'
const regex = /rain/gi;
console.log(p.replace(regex, 'snow'));
// expected output: "Its going to snow today and its going to snow tomorrow"

// Example of replacing the first occurance
console.log(p.replace('rain', 'snow'));
// expected output: "Its going to snow today and its going to rain tomorrow"

www.codegrepper.com
www.stackoverflow.com