2012年11月15日木曜日

開発環境

『初めてのJavaScript 第2版』(シェリー・パワーズ著(Shelley Powers著)、武舎 広幸+武舎 るみ訳、オライリー・ジャパン、2009年、ISBN978-4-84312-225-5) の4章(JavaScriptのJavaScriptのオブジェクト)練習問第4-2を解いてみる。

その他参考書籍

4-2.

コード(TextWrangler)

var str = "The fun of functions is that they are functional.";
var replaced_str = str.replace(/\bfun\b/g, "power");
var result = "置換前: " + str + "\n" +
  "置換後: " + replaced_str;
$('#pre0').text(result);


ちなみにPython3kの場合。

コード(TextWrangler)

sample.py

#!/usr/bin/env python3.3
#-*- coding: utf-8 -*-

import re

str = "The fun of functions is that they are functional."
pattern = re.compile(r"\bfun\b")
replaced_str = re.sub(pattern, "power", str)
print("置換前: {0}\n置換後: {1}".format(str,replaced_str))

入出力結果(Terminal)

$ ./sample.py
置換前: The fun of functions is that they are functional.
置換後: The power of functions is that they are functional.
$

0 コメント:

コメントを投稿