当前位置:首页 > 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 登录功能实现步骤 创建登录表单组件 在 Vue 项目中创建一个登录组件,通常命名为 Login.vue。该组件包含用户名和密码输入框以及提交按钮。 <template> &…

vue如何实现放大缩小

vue如何实现放大缩小

Vue实现放大缩小功能的方法 在Vue中实现元素的放大缩小功能,可以通过CSS变换、第三方库或直接操作DOM样式来实现。以下是几种常见的方法: 使用CSS transform属性 通过绑定Vue的d…

vue实现冰墩墩

vue实现冰墩墩

使用 Vue 实现冰墩墩动画 通过 Vue 的动画特性和 SVG 技术,可以动态渲染冰墩墩的形象。以下是具体实现方法: 准备 SVG 素材 冰墩墩的形象通常由多个 SVG 路径组成,包括身体轮廓…

vue实现微博发布动态

vue实现微博发布动态

使用Vue实现微博发布动态功能 创建Vue组件结构 新建一个WeiboPost.vue组件,包含文本框、图片上传和发布按钮: <template> <div class="we…

vue前端实现下载进度

vue前端实现下载进度

实现下载进度条的基本思路 在Vue中实现下载进度条,通常需要结合XMLHttpRequest或Fetch API来监听下载进度事件。通过计算已下载数据与总数据的比例,动态更新进度条的显示。 使用X…

vue实现边框

vue实现边框

Vue 实现边框的方法 在 Vue 中实现边框效果可以通过多种方式完成,包括内联样式、CSS 类绑定、动态样式以及使用第三方 UI 库。以下是几种常见的实现方法。 内联样式绑定 使用 Vue 的 :…