Category Archives: Javascript

Ajax Example

 

Calculate string length by bytes

“中文”.length in javascript will return 2 which is not the result I want, here is the replacement:

byteLength(‘中文’); will return 6.

Reference:

Javascript app object

 

Advanced Regular Expression

String:

Ventilator Set:\n FiO2 ≤ 40%\nDrug use:\n NO\nWeaning trials:\n NO\nStop reason:\n neurologic factors\n備註:\n

Purpose

Find the second \n and replace it with another character

Solution

https://regex101.com/r/t9gozM/1

Pattern Explanation: 在\n後面找一個最近的\n或是結尾之間的字串

Notes:

  • (?:)
    括號裡面的內容不產生指標($1之類的)
  • ^
    一行的開始
  • $
    一行的結束
  • (?=p)
    正前向聲明,要求接下來的字元都與樣式p相符,但是不包括比對中的那些字元
  • (?!p)
    反前向聲明,要求接下來的字元不與樣式p相符
  • 重複字元(如*)後加上? – 非貪婪的重複比對
    /a+/
    /a+?/ -> 只符合一個以上的字母a

Force https (Laravel)

Provider (AppServiceProvider)

HTML:

 

Ajax for JSON

 

C3 Chart

Official:

https://c3js.org/samples/timeseries.html

D3.js (Required)

https://d3js.org/

HTML Example:

Javascript Example:

PHP example:

 

Enter to submit a form (or trigger a button)

If user press enter in a form which has  a button with type “submit”, the form will be submitted automatically.

Prevent submitting form by pressing enter:

Add keypress event listener

 

serialize form