kumar
kumar

Reputation: 147

How to remove last two and first two characters from span element?

I'm bit loww with css. How can i remove last two and first two characters from span element?

Here is my code.

<span class="text">[[Events]]</span>

I have to remove "[[" and "]]" symbols

Upvotes: 3

Views: 654

Answers (3)

Nidhin Chandran
Nidhin Chandran

Reputation: 856

Use slice function

var y = document.getElementsByClassName('text')[0]['innerText'];
console.log(y.slice(2).slice(0,-2));
<span class="text">[[Events]]</span>

Upvotes: 0

Mohammad Javad
Mohammad Javad

Reputation: 584

function Remove(str, startIndex, count) {
    return str.substr(0, startIndex) + str.substr(startIndex + count);
}
$(document).ready(function(){
    var txt=$('span.some-span').text();
    txt=Remove(txt, 0, 2);
    txt=Remove(txt, txt.length-2, 2);
    $('span.some-span').text(txt);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<span class='some-span'>[[Events]]</span>

Upvotes: 0

Justinas
Justinas

Reputation: 43441

With js and regex ^.{2}|.{2}$

var el = document.getElementById('text');

el.innerHTML = el.innerHTML.replace(/^.{2}|.{2}$/g, '');
<span id="text">[[Events]]</span>


If just to replace [[ and ]]:

var el = document.getElementById('text');

el.innerHTML = el.innerHTML.replace(/\[|\]/g, '');
<span id="text">[[Events]]</span>

Upvotes: 3

Related Questions