Escape jquery selector value

Declare the escape function for jquery at first

$.escape = function(str) {
  return str.replace(/[!"#$%&'()*+,.\/:;<=>?@\[\\\]^`{|}~]/g, '\\$&');
};
Example code
// the value contain special character
var searchValue = 'your-value*'; 

// this is safe with escape
var list1 = $('[value="' + $.escape(searchValue) + '"]';

// this may error without escape
var list2 = $('[value="' + searchValue + '"]'; 
Try to type the input

This is escaped value

Posted in | Leave a comment

Escape regex string javascript

Declare the escape function at first

RegExp.escape = function(string) {
  return string.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&')
};
And you can search special character like this code
var patern = '^^';
var regex = new RegExp(RegExp.escape(patern));
var text = 'Hello^^';
var match = text.match(regex));
Try to type the input

Try to type the patern with special character

Matched text found:

Posted in | Leave a comment

Javacript get local date time

JS just support to get UTC time. There is how to get local time.

function GetLocalTime() {
    var date = new Date();            
    var n = date.getTime() - (date.getTimezoneOffset() * 60000);            
    return new Date(n);
}    


Leave a comment

Search

Swedish Greys - a WordPress theme from Nordic Themepark. Converted by LiteThemes.com.