Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

280
Views
How to delete the second word using : as separator on java script

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

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

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'

about 4 years ago · Juan Pablo Isaza Report

0

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(":")))

about 4 years ago · Juan Pablo Isaza Report

0

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);

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!