检查文本是否存在于vuejs中的字符串中

Check if a text exists in a string in vuejs

<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
   }
},

我正在使用 vuejs 中的代码。现在我想检查那个字符串。现在我想检查字符串是否存在 productOw ?如果 productOw 存在,检查 ?name 是否存在?那么有没有办法从字符串中获取 productOw 进行检查。因为输入 link 有不同的长短格式,所以我使用了不正确的字符串修剪。请给我你的 opinion.thanks.

您可以使用String.includes()方法。

例如,

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