当前位置:首页 > VUE

vue实现查询替换

2026-01-07 03:18:17VUE

Vue 实现查询替换功能

在 Vue 中实现查询替换功能,可以通过数据绑定和字符串操作方法结合实现。以下是具体实现方式:

基础实现

<template>
  <div>
    <textarea v-model="text" placeholder="输入文本内容"></textarea>
    <input v-model="searchText" placeholder="输入要查找的内容">
    <input v-model="replaceText" placeholder="输入替换内容">
    <button @click="replaceAll">全部替换</button>
    <div>{{ resultText }}</div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      text: '',
      searchText: '',
      replaceText: '',
      resultText: ''
    }
  },
  methods: {
    replaceAll() {
      if (!this.searchText) return
      this.resultText = this.text.replace(new RegExp(this.searchText, 'g'), this.replaceText)
    }
  }
}
</script>

高级实现(支持区分大小写)

vue实现查询替换

<template>
  <div>
    <textarea v-model="text"></textarea>
    <input v-model="searchText">
    <input v-model="replaceText">
    <label>
      <input type="checkbox" v-model="caseSensitive"> 区分大小写
    </label>
    <button @click="replaceAll">替换</button>
    <div>{{ resultText }}</div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      text: '',
      searchText: '',
      replaceText: '',
      resultText: '',
      caseSensitive: false
    }
  },
  methods: {
    replaceAll() {
      if (!this.searchText) return
      const flags = this.caseSensitive ? 'g' : 'gi'
      this.resultText = this.text.replace(new RegExp(this.searchText, flags), this.replaceText)
    }
  }
}
</script>

优化版本(实时高亮显示匹配项)

<template>
  <div>
    <textarea v-model="text"></textarea>
    <input v-model="searchText" @input="highlightMatches">
    <input v-model="replaceText">
    <button @click="replaceAll">替换</button>
    <div v-html="highlightedText"></div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      text: '',
      searchText: '',
      replaceText: '',
      highlightedText: ''
    }
  },
  methods: {
    highlightMatches() {
      if (!this.searchText) {
        this.highlightedText = this.text
        return
      }
      const regex = new RegExp(this.searchText, 'gi')
      this.highlightedText = this.text.replace(regex, match => 
        `<span style="background-color: yellow">${match}</span>`
      )
    },
    replaceAll() {
      if (!this.searchText) return
      const regex = new RegExp(this.searchText, 'gi')
      this.text = this.text.replace(regex, this.replaceText)
      this.highlightMatches()
    }
  },
  watch: {
    text() {
      this.highlightMatches()
    }
  }
}
</script>

组件化实现

vue实现查询替换

可以将查询替换功能封装为可复用组件:

<template>
  <div class="search-replace">
    <slot name="content" :text="processedText">
      <textarea v-model="internalText"></textarea>
    </slot>
    <div class="controls">
      <input v-model="searchQuery" placeholder="查找">
      <input v-model="replaceQuery" placeholder="替换为">
      <button @click="replace">替换</button>
      <button @click="replaceAll">全部替换</button>
    </div>
  </div>
</template>

<script>
export default {
  props: {
    value: String,
    caseSensitive: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      searchQuery: '',
      replaceQuery: '',
      internalText: this.value
    }
  },
  computed: {
    processedText() {
      if (!this.searchQuery) return this.internalText
      const flags = this.caseSensitive ? 'g' : 'gi'
      return this.internalText.replace(
        new RegExp(this.searchQuery, flags),
        `<mark>${this.searchQuery}</mark>`
      )
    }
  },
  methods: {
    replace() {
      // 实现单个替换逻辑
    },
    replaceAll() {
      if (!this.searchQuery) return
      const flags = this.caseSensitive ? 'g' : 'gi'
      this.internalText = this.internalText.replace(
        new RegExp(this.searchQuery, flags),
        this.replaceQuery
      )
      this.$emit('input', this.internalText)
    }
  },
  watch: {
    value(newVal) {
      this.internalText = newVal
    }
  }
}
</script>

使用组件方式:

<template>
  <search-replace v-model="documentContent" />
</template>

<script>
import SearchReplace from './components/SearchReplace.vue'

export default {
  components: {
    SearchReplace
  },
  data() {
    return {
      documentContent: '初始文本内容...'
    }
  }
}
</script>

这些实现方式覆盖了从基础到高级的查询替换功能,可以根据实际需求选择合适的方案。

标签: vue
分享给朋友:

相关文章

vue 实现排序

vue 实现排序

Vue 实现排序的方法 使用计算属性实现排序 通过计算属性对数组进行排序,可以保持原始数据不变。示例代码展示了如何对列表按名称升序排序: <template> <div>…

vue实现快手

vue实现快手

Vue 实现类似快手的功能 Vue.js 是一个渐进式 JavaScript 框架,适合构建复杂的单页应用。要实现类似快手的功能,可以结合 Vue 和相关技术栈进行开发。 核心功能模块 视频流展示…

vue实现白板

vue实现白板

Vue实现白板功能 使用Vue实现白板功能可以通过HTML5的Canvas API结合Vue的响应式特性来完成。以下是实现白板功能的关键步骤: 安装依赖 在Vue项目中安装必要的依赖,如vue-dr…

vue实现列表

vue实现列表

Vue 实现列表的方法 在 Vue 中实现列表渲染通常使用 v-for 指令,可以动态生成多个元素。以下是几种常见的实现方式: 基础列表渲染 使用 v-for 遍历数组,生成列表项。v-for 需要…

vue实现搜索查询

vue实现搜索查询

Vue 实现搜索查询的方法 双向绑定实现实时搜索 在Vue中可以通过v-model绑定输入框的值,结合计算属性或watch监听输入变化实现实时搜索。 <template> <i…

vue 实现单点登录

vue 实现单点登录

单点登录(SSO)实现原理 单点登录允许用户通过一次身份验证访问多个系统。核心原理是用户首次登录后,认证中心颁发令牌(如Token),其他系统通过验证令牌实现免登录。 Vue中实现SSO的方案 基于…