Mansoor Akhtar Mansoor Akhtar - 4 years ago 129
Javascript Question

Replace all String with empty string starting with --

i have string which is

testVariable--423h33c7uhyga5tjk


now i want to replace the above string with

testVariable


by using
javascript
replace function.

Answer Source

Use String#split method.

console.log(
  'testVariable--423h33c7uhyga5tjk'.replace('--')[0]
)


Or with String#replace method.

console.log(
  'testVariable--423h33c7uhyga5tjk'.replace(/--.*/, '')
  // or including multiline
  // .replace(/--[\s\S]*/, '')
)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download