如何让这个脚本运行多个单词

时间:2017-09-01 08:08:10

标签: javascript jquery

Heyo,

我目前仍在使用脚本。我想删除HTML标记中的一个特定单词,我无法更改标记。我搜索了一个解决方案并找到了这个脚本:

var webpack = require('webpack');
var path = require('path');

var htmlWebpackPlugin = require('html-webpack-plugin');
var autoprefixer = require('autoprefixer');

var config = {
  entry: {
    index: [
      'webpack-dev-server/client?http://0.0.0.0:8080',
      'webpack/hot/only-dev-server',
      path.resolve(__dirname, 'src/index.jsx')
    ],
    introduction: [
      'webpack-dev-server/client?http://0.0.0.0:8080',
      'webpack/hot/only-dev-server',
      path.resolve(__dirname, 'src/introduction.jsx')
    ],
  },
  output: {
    path: path.resolve(__dirname, 'build'),
    publishPath: '/',
    filename: 'js/[name].js'
  },
  module: {
    loaders: [
      {
        test:/\.jsx?$/,
        exclude: /node_modules/,
        loader: 'react-hot!babel'
      },
      {
        test: /\.sass$/,
        execlude: /node_modules/,
        loader: 'style!css!postcss!sass'
      },{
        test: /\.scss$/,
        execulde: /node_modules/,
        loader: 'style!css!postcss!sass'
      },
      {
        test: /\.css$/,
        exclude: /node_modules/,
        loader: 'style!css!postcss',
      },
      {
        test: /\.(jpg|png|gif)$/,
        exclude: /node_modules/,
        loader: 'file?name=images/[name].[ext]'
      },
      {
        test: /\.(woff|woff2)(\?v=\d+\.\d+\.\d+)?$/,
        loader: 'url?limit=10000&mimetype=application/font-woff'
      },
      {
        test: /\.ttf(\?v=\d+\.\d+\.\d+)?$/,
        loader: 'url?limit=10000&mimetype=application/octet-stream'
      },
      {
        test: /\.eot(\?v=\d+\.\d+\.\d+)?$/,
        loader: 'file'
      },
      {
        test: /\.svg(\?v=\d+\.\d+\.\d+)?$/,
        loader: 'url?limit=10000&mimetype=image/svg+xml'
      }
    ]
  },
  postcss: [
    autoprefixer({ browsers: ['last 2 versions']})
  ],
  resolve: {
    extensions: ['', '.js', '.jsx']
  },
  exclude: [
    /(test)/
  ],
  devServer: {
    contenBase: './dist'
  },
  plugins: [
    new webpack.HotModuleReplacementPlugin(),
    new htmlWebpackPlugin({
      filename: 'index.html',
      template: './src/html/index.html'

    }),
    new htmlWebpackPlugin({
      filename: 'introduction.html',
      template: './src/html/introduction.html'

    }),

  ]
}

module.exports = config;

现在这个脚本适用于一个单词($(':contains("bad")').each(function(){ $(this).html($(this).html().split("bad").join("")); }); ),但是我想用这个删除bad个单词脚本。所以我尝试了这个:

mutliple

但这根本不起作用。

这是一个工作片段:

< div class =“snippet”data-lang =“js”data-hide =“false”data-console =“true”data-babel =“false”>
$(':contains("bad", "good")').each(function(){
    $(this).html($(this).html().split("bad","good").join(""));
});
$(':contains("bad")').each(function(){
		$(this).html($(this).html().split("bad").join(""));
	});

如何让它发挥作用?

2 个答案:

答案 0 :(得分:4)

只需.replace(/bad|good/g, '')。这里/g表示替换所有匹配,而不仅仅是第一个匹配。 bad|good表示替换其中任何一个。

&#13;
&#13;
$(document).ready(function () {
  $('#text').text($('#text').text().replace(/good|bad/g, ''));
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<span id="text">Some words are not as good or not as bad, but bad words may be good too.</span>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

你可以试试这个:

 var replaced = $('#text').text().replace("good", '').replace("bad", '');
 $('#text').text(replaced);
相关问题
最新问题