2015-12-02 14 views
6

मैं अपने तत्व से सभी ­ संस्थाओं (नरम हाइफ़न) को हटाने के लिए कोशिश कर रहा हूँ से ­ (नरम हाइफ़न) इकाई निकालें,तत्व

मैं इस jQuery का उपयोग कर की कोशिश कर रहा हूँ। जब मैं इकाई वाले एचटीएमएल तत्व से पाठ प्राप्त करता हूं तो मुझे लगता है कि इकाई एक "स्ट्रिंग" है या संपादित नहीं की जा सकती है।

क्या आपको विशेष रूप से इकाइयों समेत एक स्ट्रिंग प्राप्त करने के लिए कुछ करना है?

$(document).ready(function(){ 
 
    $("button").on("click", function(){ 
 
    var html = $("div > span").html(); 
 
    var newHtml = html.replace("­", ""); 
 
    
 
    $("div > span").html(newHtml); 
 
    }); 
 
});
div{ 
 
    max-width: 50px; 
 
    padding: 10px; 
 
    border: 1px solid #000; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div> 
 
    <span>My text will be hyphe&shy;ned</span> 
 
</div> 
 
<button> 
 
    Remove Hyphens 
 
</button>

उत्तर

6

उपयोग regex:

var newHtml = html.replace(/\&shy;/gi, ""); 

नोट: तुम भी, क्योंकि ब्राउज़र भी मानव अनुकूल वर्ण में संख्या में व्यवहार करता है और नहीं &#173; के लिए जाँच करने के लिए आवश्यकता हो सकती है।

स्पष्टीकरण:

/(\&shy;)/gi 
    1st Capturing group (\&shy;) 
    \& matches the character & literally 
    shy; matches the characters shy; literally (case insensitive) 
    g modifier: global. All matches (don't return on first match) 
    i modifier: insensitive. Case insensitive match (ignores case of [a-zA-Z]) 

स्निपेट

$(document).ready(function(){ 
 
    $("button").on("click", function(){ 
 
    var html = $("div > span").html(); 
 
    var newHtml = html.replace(/(\&shy;|­|&#173;)/gi, ""); 
 
    
 
    $("div > span").html(newHtml); 
 
    }); 
 
});
div{ 
 
    max-width: 50px; 
 
    padding: 10px; 
 
    border: 1px solid #000; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div> 
 
    <span>My text will be hyphe&shy;ned</span> 
 
</div> 
 
<button> 
 
    Remove Hyphens 
 
</button>

Regex101: https://regex101.com/r/wD3oX7/1

+1

धन्यवाद। यह पूरी तरह से काम किया :) –

संबंधित मुद्दे