正则表达式过滤跨度标签(如果具有属性)

时间:2019-02-14 07:17:04

标签: java regex

如果没有使用Java的任何属性,我在下面的代码中要剥离或除去span标记。

此正则表达式删除所有SPAN标记。 <(/)?[] span [^>] >

例如

<span style="font-weight: bold;text-decoration-line: underline;">test</span><p><span>&nbsp;</span></p><p><span>Table</span></p>

输出:

  <span style="font-weight: bold;text-decoration-line: underline;">test</span><p>&nbsp;</p><p>Table</p>

有帮助吗?

2 个答案:

答案 0 :(得分:0)

不可能。正则表达式无法知道哪个</span>结束标记属于您要删除的<span>。使用HTML解析器,例如jsoup

编辑:

示例

    String html = "<span style=\"font-weight: bold;text-decoration-line: underline;\">test</span><p><span>&nbsp;</span></p><p><span>Table</span></p>";

    Document doc = Jsoup.parse(html);

    for (Element span : doc.getElementsByTag("span")) {
        if (span.attributes().size() == 0) {
            span.unwrap();
        }
    }

    doc.outputSettings().prettyPrint(false);

    String result = doc.body().html();

答案 1 :(得分:-1)

尝试使用Java代码

 <template>
  <div>
    <v-btn raised @click="onPickItemFile(index)">base64</v-btn>
    <input type="file" @change="onChange" :multiple="multiple" 
     style="display: none" ref="foobar_y" />
   </div>
 </template>

<script>
 export default {
 props: {
  multiple: {
    default: false,
    type: Boolean,
  },
  done: {
    type: Function,
    default: () => {}
  },
  index: '',
},
methods: {
  onChange(e){
    // get the files
    let files = e.target.files;
    // Process each file
    var allFiles = []
    for (var i = 0; i < files.length; i++) {
      let file = files[i]
      // Make new FileReader
      let reader = new FileReader()
      // Convert the file to base64 text
      reader.readAsDataURL(file)
      // on reader load somthing...
      reader.onload = () => {
        // Make a fileInfo Object
        let fileInfo = {
          name: file.name,
          type: file.type,
          size: Math.round(file.size / 1000)+' kB',
          base64: reader.result,
          file: file
        }
        let index = this.index
        // Push it to the state
        allFiles.push(fileInfo)
        // If all files have been proceed
        if(allFiles.length == files.length){
          // Apply Callback function
          if(this.multiple) this.done(allFiles)
          else this.done(allFiles[0])
        }
      } // reader.onload
    } // for
  }, // onChange()
  onPickItemFile (index) {
    this.$refs.foobar_y.click()
  }, 
  }
 };
</script>