我用css有点低。如何从span元素中删除最后两个和前两个字符?
这是我的代码。
<span class="text">[[Events]]</span>
我必须删除&#34; [[&#34;和&#34;]]&#34;符号
答案 0 :(得分:3)
使用js和正则表达式 <?php
mb_internal_encoding('UTF-8');
$database = 'university';
$collection = 'students';
/**
* MongoDB connection
*/
try{
// Connecting to server
$m = new MongoClient( );
}catch(MongoConnectionException $connectionException){
print $connectionException;
exit;
}
$m_collection = $m->$database->$collection;
/**
* Define the document fields to return to DataTables (as in http://us.php.net/manual/en/mongocollection.find.php).
* If empty, the whole document will be returned.
*/
$fields = array();
// Input method (use $_GET, $_POST or $_REQUEST)
$input = & $_REQUEST;
/**
* Handle requested DataProps
*/
// Number of columns being displayed (useful for getting individual column search info)
$iColumns = $input['iColumns'];
// Get mDataProp values assigned for each table column
$dataProps = array();
for ($i = 0; $i < $iColumns; $i++) {
$var = 'mDataProp_'.$i;
if (!empty($input[$var]) && $input[$var] != 'null') {
$dataProps[$i] = $input[$var];
}
}
/**
* Filtering
* NOTE this does not match the built-in DataTables filtering which does it
* word by word on any field. It's possible to do here, but concerned about efficiency
* on very large collections.
*/
$searchTermsAny = array();
$searchTermsAll = array();
if ( !empty($input['sSearch']) ) {
$sSearch = $input['sSearch'];
for ( $i=0 ; $i < $iColumns ; $i++ ) {
if ($input['bSearchable_'.$i] == 'true') {
if ($input['bRegex'] == 'true') {
$sRegex = str_replace('/', '\/', $sSearch);
} else {
$sRegex = preg_quote($sSearch, '/');
}
$searchTermsAny[] = array(
$dataProps[$i] => new MongoRegex( '/'.$sRegex.'/i' )
);
}
}
}
// Individual column filtering
for ( $i=0 ; $i < $iColumns ; $i++ ) {
if ( $input['bSearchable_'.$i] == 'true' && $input['sSearch_'.$i] != '' ) {
if ($input['bRegex_'.$i] == 'true') {
$sRegex = str_replace('/', '\/', $input['sSearch_'.$i]);
} else {
$sRegex = preg_quote($input['sSearch_'.$i], '/');
}
$searchTermsAll[ $dataProps[$i] ] = new MongoRegex( '/'.$sRegex.'/i' );
}
}
$searchTerms = $searchTermsAll;
if (!empty($searchTermsAny)) {
$searchTerms['$or'] = $searchTermsAny;
}
$cursor = $m_collection->find($searchTerms, $fields);
/**
* Paging
*/
if ( isset( $input['iDisplayStart'] ) && $input['iDisplayLength'] != '-1' ) {
$cursor->limit( $input['iDisplayLength'] )->skip( $input['iDisplayStart'] );
}
/**
* Ordering
*/
if ( isset($input['iSortCol_0']) ) {
$sort_fields = array();
for ( $i=0 ; $i<intval( $input['iSortingCols'] ) ; $i++ ) {
if ( $input[ 'bSortable_'.intval($input['iSortCol_'.$i]) ] == 'true' ) {
$field = $dataProps[ intval( $input['iSortCol_'.$i] ) ];
$order = ( $input['sSortDir_'.$i]=='desc' ? -1 : 1 );
$sort_fields[$field] = $order;
}
}
$cursor->sort($sort_fields);
}
/**
* Output
*/
$output = array(
"sEcho" => intval($input['sEcho']),
"iTotalRecords" => $m_collection->count(),
"iTotalDisplayRecords" => $cursor->count(),
"aaData" => array(),
);
foreach ( $cursor as $doc ) {
$output['aaData'][] = $doc;
}
echo json_encode( $output );
^.{2}|.{2}$
var el = document.getElementById('text');
el.innerHTML = el.innerHTML.replace(/^.{2}|.{2}$/g, '');
如果只是要替换<span id="text">[[Events]]</span>
和[[
:
]]
var el = document.getElementById('text');
el.innerHTML = el.innerHTML.replace(/\[|\]/g, '');
答案 1 :(得分:0)
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>
答案 2 :(得分:0)
使用切片功能
var y = document.getElementsByClassName('text')[0]['innerText'];
console.log(y.slice(2).slice(0,-2));
<span class="text">[[Events]]</span>