Home > Blockchain >  Check if a text exists in a string in vuejs
Check if a text exists in a string in vuejs

Time:04-20

<a-form-item label="Link" :colon="false">
     <a-input placeholder="Link" @blur="(e) => valueInput(e)" />
</a-form-item>

// script

methods: {
   valueInput(e) {
      console.log(e.target.value) 
       //result 1 : https://web-sand.io/browse/productOw/next-tip
       //result 2 : https://web-sand.io/browse/productOw/next-tip?name='next'
       //result 3 : https://web-sand.io/browse/next-tip
   }
},

I'm working with code in vuejs. Now I want to check that string. Now I want to check if the string exists productOw ? and if productOw exists, check if ?name exists? So is there a way to get productOw from the string to check. Because the input link has a different length and short format, I used incorrect string trimming. Please give me your opinion.thanks.

CodePudding user response:

You can use the String.includes() method.

For example,

const str1 = `https://web-sand.io/browse/productOw/next-tip`
const str2 = `https://web-sand.io/browse/productOw/next-tip?name='next'`
const str3 = `https://web-sand.io/browse/next-tip`

//prints 'no productOw' if it does not include 'productOw'.
//if 'productOw' exists, checks if '?name' exists, then prints boolean accordingly.
console.log(str1.includes('productOw') ? str1.includes('?name') : 'no productOw')
console.log(str2.includes('productOw') ? str2.includes('?name') : 'no productOw')
console.log(str3.includes('productOw') ? str3.includes('?name') : 'no productOw')

  • Related