JQuery删除字符串之前的文本

时间:2015-01-26 04:17:34

标签: javascript jquery regex

无论如何都要删除fa之前的字符 - ?

[]

fa-deviantart
[]

fa-diamond
[]

fa-digg
[]

fa-dollar
(alias)
[]

fa-dot-circle-o
[]

fa-download
[]

fa-dribbble
[]



$(document).ready(function() {
  // var myfa = "fa-";
  // var findFA = myfa.substr(myfa.length - 3); // => "fa-"
  
  $("textarea").val( $("div").html().replace(/fa-/g,".fa .fa-") );
  $("div").html( $("textarea").val() );
});

div {
  white-space: pre-line;
  position: absolute;
  top: 200px;
  top: 100px;
}

textarea {
  position: absolute;
  width: 90%;
  left: 5%;
  padding: 0;
  height: 100px;
}

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="IE=9" />
    <link type="text/css" rel="stylesheet" href="http://necolas.github.io/normalize.css/3.0.1/normalize.css" />
    <script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>
  </head>
  <body>
    <textarea></textarea>
    <div>
      
      fa-bitcoin
      (alias)
      []
      
      fa-bold
      []
      
      fa-bolt
      []
      
      fa-bomb
      []
      
      fa-book
      []
      
      fa-bookmark
      []
      
      fa-bookmark-o
      []
      
      fa-briefcase
      []
      
      fa-btc
      []
      
      fa-bug
      []
      
      fa-building
      []
      
      fa-building-o
      []
      
      fa-bullhorn
      []
      
      fa-bullseye
      []
      
      fa-bus
      []
      
      fa-buysellads
      []
      
      fa-cab
      (alias)
      []
      
      fa-calculator
      []
      
      fa-calendar
      []
      
      fa-calendar-o
      []
      
      fa-camera
      []
      
      fa-camera-retro
      []
      
      fa-car
      []
      
      fa-caret-down
      []
      
      fa-caret-left
      []
      
      fa-caret-right
      []
      
      fa-caret-square-o-down
      []
      
      fa-caret-square-o-left
      []
      
      fa-caret-square-o-right
      []
      
      fa-caret-square-o-up
      []
      
      fa-caret-up
      []
      
      fa-cart-arrow-down
      []
      
      fa-cart-plus
      []
      
      fa-cc
      []
      
      fa-cc-amex
      []
      
      fa-cc-discover
      []
      
      fa-cc-mastercard
      []
      
      fa-cc-paypal
      []
      
      fa-cc-stripe
      []
      
      fa-cc-visa
      []
      
      fa-certificate
      []
      
      fa-chain
      (alias)
      []
      
      fa-chain-broken
      []
      
      fa-check
      []
      
      fa-check-circle
      []
      
      fa-check-circle-o
      []
      
      fa-check-square
      []
      
      fa-check-square-o
      []
      
      fa-chevron-circle-down
      []
      
      fa-chevron-circle-left
      []
      
      fa-chevron-circle-right
      []
      
      fa-chevron-circle-up
      []
      
      fa-chevron-down
      []
      
      fa-chevron-left
      []
      
      fa-chevron-right
      []
      
      fa-chevron-up
      []
      
      fa-child
      []
      
      fa-circle
      []
      
      fa-circle-o
      []
      
      fa-circle-o-notch
      []
      
      fa-circle-thin
      []
      
      fa-clipboard
      []
      
      fa-clock-o
      []
      
      fa-close
      (alias)
      []
      
      fa-cloud
      []
      
      fa-cloud-download
      []
      
      fa-cloud-upload
      []
      
      fa-cny
      (alias)
      []
      
      fa-code
      []
      
      fa-code-fork
      []
      
      fa-codepen
      []
      
      fa-coffee
      []
      
      fa-cog
      []
      
      fa-cogs
      []
      
      fa-columns
      []
      
      fa-comment
      []
      
      fa-comment-o
      []
      
      fa-comments
      []
      
      fa-comments-o
      []
      
      fa-compass
      []
      
      fa-compress
      []
      
      fa-connectdevelop
      []
      
      fa-copy
      (alias)
      []
      
      fa-copyright
      []
      
      fa-credit-card
      []
      
      fa-crop
      []
      
      fa-crosshairs
      []
      
      fa-css3
      []
      
      fa-cube
      []
    </div>
  </body>
</html>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:2)

您可以使用正则表达式匹配以fa-开头的有效类名。

//A string you want to extract the classes from.
var exampleString = 'fa-deviantart[]fa-diamond[]';

//Extract the class names using a regex into an array.
var classList = exampleString.match(/fa\-[a-zA-Z0-9\-_]*/g);

//Create a new string from the array of matched classes.
alert(classList.join(' '));