replace いろいろ

  1. テキストで書き換える

  2. 外部リンクに target を付与する

1) テキストで書き換える

<script><!--
//<!-- class chk12 の内容を、テキストデータ「js_edit chk12」に置き換える
jQuery(document).ready(function() {
        jQuery('.chk12').text('js_edit chk12');
//<!-- class chk14 の内容を、HTMLで「<section>JS 書き換え chk14<br>html で br も可能</section>」に置き換える
        jQuery('.chk14').html('<section>JS 書き換え chk14<br>html で br も可能</section>');
	});
//--></script>
最短?書き換え例;住所表示デフォルト表示を変えたい
テキスト「郡市区」の文字列を「市区町村以降」に置換
$('.tds_add').text($('.tds_add').text().replace(/郡市区/g, '市区町村以降'));

2) 外部リンクに target を付与する

既存スクリプトの中から。
<script><!--
$(window).load(function () {
	$(".link").each(function(){
   $(this).html(
$(this).html().replace(/((http|https|ftp):\/\/[\w?=&.\/-;#~%-]+(?![\w\s?&.\/;#~%"=-]*>))/g, '<a href="$1" target="_blank">$1</a> ') );
 });
});
//--></script>
.link が付いてたら、target _blank 付ける、らしい。

ということは。a で、each function なのでは。探してみた↓
<script><!--
window.addEventListener('load', function () { //最後に発動
	var host = new RegExp('/' + window.location.host + '/');
	$('a').each(function () {
		if (!host.test(this.href)) {
			var $this = $(this);
			$this.attr('target', '_blank'); // 外部リンクに target=_blank を付ける
		}
	});
});
//--></script>

“/((http|https|ftp):\/\/[\w?=&.\/-;#~%-]+(?![\w\s?&.\/;#~%"=-]*>))/g”
こんなに頑張って、正規表現する必要ありません。

やみくもに設定するのではなく、どういうパターンなのかを、考えろよ。
って思うけど、もう、コレ書いたであろう社員は辞めちゃってるので、サックリとクソスクリプトは抹殺。

いいなと思ったら応援しよう!