Skip to content

Instantly share code, notes, and snippets.

View outiy5's full-sized avatar

Antony Wood outiy5

View GitHub Profile
@outiy5
outiy5 / a_href.html
Last active March 19, 2020 09:00
[use javascript in <a> tag] 如何在 < a &gt; tag 里面使用javascript生成的地址? a href encodeuri #javascript
<!-- https://stackoverflow.com/questions/5846137/script-inside-href-how-to -->
<a href="javascript:location='http://www.linkimprov.com/?ref='+encodeURI(location.href.match(/(?=https?:\/\/)?(?=www\.)?(.*)/)[1]).substring(7);"></a>
@outiy5
outiy5 / greey_non.js
Last active March 19, 2020 08:56
[greey mode and non-greedy mode] 正则表达式中的贪婪模式和截断模式 #javascript #regex
// https://stackoverflow.com/questions/14213848/difference-between-and
// Both will match any sequence of one or more characters. The difference is that:
// .+ is greedy and consumes as many characters as it can.
// .+? is reluctant and consumes as few characters as it can.
var r0 = /\$[\s\S]+\$/g;
r0.exec('$s\nda$sd\n$') // match: $s\nda$sd\n$
var r1 = /\$[\s\S]+?\$/g;
r1.exec('$s\ndasd\n$') // match: $s\nda$
@outiy5
outiy5 / regex_multiline.js
Last active March 19, 2020 08:53
[regex for multiline] 如何使用正则表达式匹配多行字符串? #javascript #regex
// https://stackoverflow.com/questions/1979884/how-to-use-javascript-regex-over-multiple-lines
// use ? for non-greedy mode
// () use to capture
var regex = /\$([\s\S]+?)\$/g;
regex.exec('$s\ndasd\n$');
@outiy5
outiy5 / js_literal_string.js
Created March 19, 2020 08:46
[how to convert javascript literal to string] js字面量如何当做字符串使用? #javascript
// This one is wrong
<script type="text/javascript">document.write('<img src="http://chart.apis.google.com/chart?cht=tx&chl=' + escape('\\\\frac{1}{2}').replace(' ',"%20") + '" alt="">');</script>
// This one is right
// learn it from https://katex.org/docs/api.html
<script type="text/javascript">document.write('<img src="http://chart.apis.google.com/chart?cht=tx&chl=' + String.raw`$1`.replace(' ',"%20") + '" />');</script>
@outiy5
outiy5 / parcel_relative.sh
Created March 19, 2020 08:42
[parcel generate relative path] use parcel to generate relative path, so url can be opened directly, without server #javascript
//https://github.com/parcel-bundler/parcel/issues/470
parcel build index.html --public-url ./