2015-04-07 21 views
-2

這裏是我的搜索和替換我想出了。如何結合註釋或警報來描述輸入的模式和功能框?任何建議表示讚賞!Javascript搜索和替換需要合併更多

Uncaught SyntaxError: Unexpected reserved word 

這似乎是一個Java類,它將無法運行:

<html> 
<head> 
<title> Search & Replace</title> 
</head> 
<body> 

<script language="javascript"> 

import java.util.regex.Pattern; 
import java.util.regex.Matcher; 

public class StringReplace { 
    public static void main(String[] args) { 
     String source = "The quick blue bird flies over the brown baseball field."; 
     String find = "blue"; 
     String replace = "brown"; 

     // 
     // Compiles the given regular expression into a pattern 
     // 
     Pattern pattern = Pattern.compile(find); 

     // 
     // Creates a matcher that will match the given input against the pattern 
     // 
     Matcher matcher = pattern.matcher(source); 

     // 
     // Replaces every subsequence of the input sequence that matches the 
     // pattern with the given replacement string 
     // 
     String output = matcher.replaceAll(replace); 

     System.out.println("Source = " + source); 
      System.out.println("Output = " + output); 
    } 
} 
</script> 
</body> 
</html> 
+3

「Java」是「JavaScript」,因爲「火腿」是「倉鼠」。 – lxe

回答

0

這不是有效的JavaScript,如果你嘗試和進口加載這個頁面的控制檯將拋出一個錯誤在瀏覽器中,除非你使用像jsp這樣的技術。

有幾個庫已經會做這樣的字符串的東西。嘗試stringjs例如:http://stringjs.com/。但是,如果你想在整個頁面上運行它,你需要解析更復雜的DOM(這樣做對於字符串來說更容易)。

對於使用輸入,您應該深入研究html。有許多文本框和文本框選項,您可以綁定到JavaScript。