Home > OS >  Vuejs: Handling slicing of select results
Vuejs: Handling slicing of select results

Time:03-29

let slug = 'test-value/2';
let slug = 'test-value-data/10';
let slug = 'test-number-data/100';
let slug = 'data-test/1000';
let slug = 'test/10000';

For example, each time I select, I will get 1 slug as above. Now I want to trim the string from /number. For example, I want to trim the string test-value/2 to test-value. So how to cut the string from / onwards. And common to all slugs. Please give me your opinion, Thank you.

CodePudding user response:

Try to replace it using regex as shown in the following example :

let slug='test-value/2'
let trimmedSlug=slug.replace(/\/.*/i,'')
console.log(trimmedSlug)

CodePudding user response:

You can use .split()

Note: Use this method if the slug will always have a format of <slug without '/'>/<number>

// Using .split();

let slug = 'test-value/2';
const trimmedSlug = slug.split('/')[0];

console.log(trimmedSlug);

  • Related