-1
<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.

deskeay
  • 263
  • 2
  • 15
  • This is not a vue.js-specific problem. This is an extremely basic Javascript problem. [How to check whether a string contains a substring in JavaScript?](https://stackoverflow.com/questions/1789945/how-to-check-whether-a-string-contains-a-substring-in-javascript) – Clonkex Apr 20 '22 at 03:13

1 Answers1

3

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')
cSharp
  • 2,884
  • 1
  • 6
  • 23