js processing data

Posted by rockinaway on Wed, 09 Oct 2019 17:19:22 +0200

Handling of web address

`https://www.awiizarii.com/index.php?ctl=Goods_Goods&met=goods&type=goods&gid=43558`

Get the value of the parameters in the web site

function getUrlParam(name){

    var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
    var r = window.location.search.substr(1).match(reg);
    if (r!=null) return unescape(r[2]); return null;
}

getUrlParam(gid) // Gets a value of 43558

Modify the value of the parameters in the web site

  function replaceParamVal(paramName,replaceWith) {
            var oUrl = this.location.href.toString();
            var re=eval('/('+ paramName+'=)([^&]*)/gi');
            var nUrl = oUrl.replace(re,paramName+'='+replaceWith);
            // this.location = nUrl;
            return nUrl
        }
     replaceParamVal("gid",6)//https://www.awiizarii.com/index.php?ctl=Goods_Goods&met=goods&type=goods&gid=6

Modify the Web address on the page without refreshing the browser

 

let historyJs = replaceParamVal("gid",data.data.goods_id)
     history.pushState(null, null, historyJs);
     

Conversion of time format

Converting a timestamp to a time format

formatDate(timestamp) {

    var date = new Date(timestamp);//The time stamp is 10 bits and needs * 1000. If the time stamp is 13 bits, it does not need to multiply 1000.
    var Y = date.getFullYear() + '-';
    var M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1) + '-';
    var D = (date.getDate() < 10 ? '0'+(date.getDate()) : date.getDate()) + ' ';
    var h = (date.getHours() < 10 ? '0'+(date.getHours()) : date.getHours()) + ':';
    var m = (date.getMinutes() < 10 ? '0'+(date.getMinutes()) : date.getMinutes()) + ':';
    var s = (date.getSeconds() < 10 ? '0'+(date.getSeconds()) : date.getSeconds());
    return Y+M+D+h+m+s;
},

Converting access to computer time into usable time

function time(){

      var d=new Date();
      var year=d.getFullYear();
      var month=change(d.getMonth()+1);
      var day=change(d.getDate());
      var hour=change(d.getHours());
      var minute=change(d.getMinutes());
      var second=change(d.getSeconds());
      function change(t){
        if(t<10){
         return "0"+t;
        }else{
         return t;
        }
      }
      var time=year+'-'+month+'-'+day+' '+hour+':'+minute+':'+second;
    return time
}

Method of Processing String Data

String to Object Conversion

var obj = JSON.parse(a)

Extract a part of the string and return a line of string (same as substring) [slice]

var arr1 = a.slice(1,4)
console.log(arr1)
//ell

By dividing the string into substrings, a string is made into an array of strings. (split)

var split = a.split("")
console.log(spliit)
// ["h","e","l","l","0"]

Combine the text of two or more characters and return a new string (concat)

 var a = "hello";
 var b = ",world";
 var c = a.concat(b)
 console.log(c) //"hello,world"
 

Returns the index that appears at the first place of a substring in the string (search from left to right). If there is no match, return - 1 (indexOf)

  var index = a.indexOf("e") 
  consolg.log(index) //  1

  var index2 = a.indexOf("j")
  console.log(index2) // -1
  

Returns the character at the specified position (charAt)

 var char = a.charAt (3)
 console.log(char) // 'l'

Returns the index at the end of the neutron string (right-to-left search), and - 1 (last IndexOf) if no matches are found.

var indexOf = a.lastIndexOf("l")
console.log(indexOf) // 3

Check that a string matches the contents of a regular expression and returns null(match) if no match is found.

var re = new RegExp(/^\w+$/);
var is_alpha1 = a.match(re);
console.log(is_alphal) //  "hello"

Returns a substring of a string whose input parameter is from the beginning to the end. (subString)

var sub = a.subString(2,4)
console.log(sub) // "lo"

Returns a substring of the string, the input parameter being the starting position and length. (subStr)

var subStr = a.subStr(1,3)
console.log(subStr)
//  ell

Find a string that matches a regular expression, and replace the matched string with a new string.

var replace = a.replace(re,"Hello")
console.log(replace)
//Hello

Convert the entire string to a lowercase letter (toLowerCase)

 var lowerCase = a.toLowerCase();
console.log(lowerCase)
//hello

Convert the entire string to upperCase
var upperCase = a.toUpperCase
console.log(upperCase)
//HELLO

Array Processing Method

Array join
var e = [1,2,3,4,5,6]
var b = a.join("");
console.log(b)
// "123456"

Topics: Javascript PHP JSON