so i have string like this
accept : menerima
accuse : menuduh
achieve : mencapai
acquire : memperoleh
adapt : menyesuaikan
add : menambahkan
and how to delete the second word using : as separator, And make the result like this.
accept
accuse
achieve
acquire
adapt
add
thanks
A regex will work.
const rx = /\s+:.*$/mg
str.replace(rx, '')
https://regex101.com/r/sV2TnQ/1
The g flag will replace multiple occurances. The m flag lets $ match end of line or document.
So 'spaces', a 'colon', and 'all to end of line'
try like this,
const str = 'accept : menerima'
console.log(str.slice(0,str.indexOf(":")))
You can create one method, which will take string as input and will return new string.
const txt = 'accept : menerima'
function getCroppedString(str){
return str.slice(0,str.indexOf(":"));
}
getCroppedString(txt)
console.log(getCroppedString(txt));
const str = 'accept : menerima'
console.log(str.slice(0,str.indexOf(":")))
A different approach to a regex would be to loop over the lines, and split the : using a map.
const string = `accept : menerima
accuse : menuduh
achieve : mencapai
acquire : memperoleh
adapt : menyesuaikan
add : menambahkan`;
const lines = string.split("\n")
.map((line) => line.split(" : ")[0])
// Optionally if you would like to return the output as a single string
.join("\n");
console.log(lines);