regular-tester.vue 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <template>
  2. <a-modal width="90%" height="90%" v-model:visible="visible" title="正则测试" :footer="false" @cancel="handleCancel">
  3. <a-row class="wrapper">
  4. <a-col :span="24">
  5. <a-form ref="formRef" class="form" :label-col-props="{ span: 2 }" :wrapper-col-props="{ span: 21 }">
  6. <a-form-item label="测试文本" placeholder="在此输入待匹配文本" row-class="keep-margin">
  7. <a-textarea class="textarea" v-model="content" auto-size />
  8. <template #extra>
  9. </template>
  10. </a-form-item>
  11. <a-form-item label="正则" placeholder="需要匹配的正则表达式,一行一个" row-class="keep-margin">
  12. <a-textarea class="textarea" :auto-size="{ minRows: 5, maxRows: 20 }" v-model="pattern" />
  13. <template #extra>
  14. <a-alert type="success" v-if="testResult === true">正则匹配</a-alert>
  15. <a-alert type="error" v-else-if="testResult === false">正则不匹配</a-alert>
  16. <a-alert type="error" v-else-if="testResult instanceof Error">
  17. 发生错误:{{ testResult.message }}</a-alert>
  18. </template>
  19. </a-form-item>
  20. <a-form-item>
  21. <a-space>
  22. <a-button type="primary" @click="handleTester">测试</a-button>
  23. <a-button type="secondary" @click="handleCancel">取消</a-button>
  24. </a-space>
  25. </a-form-item>
  26. </a-form>
  27. </a-col>
  28. </a-row>
  29. </a-modal>
  30. </template>
  31. <script lang="ts" setup>
  32. import { defineEmits, ref } from 'vue';
  33. import { Message } from '@arco-design/web-vue';
  34. import {
  35. TkPoolParams,
  36. queryTkPool,
  37. TkPoolRecord,
  38. } from '@/api/taobao';
  39. const content = ref<string>('');
  40. const pattern = defineModel<string>('pattern', { default: '', required: true })
  41. const visible = defineModel<boolean>('visible', { default: false, required: true })
  42. const testResult = ref<boolean | undefined | Error>(undefined)
  43. const testPatterns = (text: string, patterns: string): boolean | Error => {
  44. try {
  45. const patternArray = patterns.split('\n').filter(p => p.trim() !== '');
  46. const regexes = patternArray.map(p => new RegExp(p, 'g'));
  47. return regexes.some(regex => regex.test(text));
  48. } catch (error: any) {
  49. const msg = (error as Error).message
  50. console.error("正则表达式无效:", msg)
  51. return error as Error
  52. }
  53. };
  54. const handleTester = () => {
  55. console.log('test');
  56. const msg = 'test'
  57. testResult.value = testPatterns(content.value, pattern.value)
  58. };
  59. const handleCancel = () => {
  60. testResult.value = undefined
  61. visible.value = false;
  62. }
  63. testResult.value = undefined
  64. </script>
  65. <style scoped lang="less">
  66. .container {
  67. padding: 0 20px 20px 20px;
  68. }
  69. .wrapper {
  70. padding: 20px 0 0 20px;
  71. min-height: 580px;
  72. background-color: var(--color-bg-2);
  73. border-radius: 4px;
  74. }
  75. :deep(.section-title) {
  76. margin-top: 0;
  77. margin-bottom: 16px;
  78. font-size: 14px;
  79. }
  80. .form {
  81. margin: 0 auto;
  82. }
  83. .form .textarea {
  84. height: 100%;
  85. min-height: 200px;
  86. }
  87. </style>