4

次のように改行とタブを含む長い文字列があるとします。

var x = "This is a long string.\n\t This is another one on next line.";

では、正規表現を使用して、この文字列をトークンに分割するにはどうすればよいでしょうか。

.split(' ')Javascriptの正規表現を学びたいので使いたくありません。

より複雑な文字列は次のようになります。

var y = "This @is a #long $string. Alright, lets split this.";

ここで、特殊文字や句読点を使用せずに、この文字列から有効な単語のみを抽出したいと思います。つまり、次のようにします。

var xwords = ["This", "is", "a", "long", "string", "This", "is", "another", "one", "on", "next", "line"];

var ywords = ["This", "is", "a", "long", "string", "Alright", "lets", "split", "this"];
4

6 に答える 6

9

これは、あなたが尋ねたもののjsfiddleの例です:http://jsfiddle.net/ayezutov/BjXw5/1/

基本的に、コードは非常に単純です。

var y = "This @is a #long $string. Alright, lets split this.";
var regex = /[^\s]+/g; // This is "multiple not space characters, which should be searched not once in string"

var match = y.match(regex);
for (var i = 0; i<match.length; i++)
{
    document.write(match[i]);
    document.write('<br>');
}

更新: 基本的に、区切り文字のリストを展開できます: http://jsfiddle.net/ayezutov/BjXw5/2/

var regex = /[^\s\.,!?]+/g;

更新 2: 常に文字のみ: http://jsfiddle.net/ayezutov/BjXw5/3/

var regex = /\w+/g;
于 2011-12-09T06:49:34.590 に答える
2

\s+文字列をトークン化するために使用します。

于 2011-12-09T06:34:26.627 に答える
2

execは、一致をループして、単語以外の(\ W)文字を削除できます。

var A= [], str= "This @is a #long $string. Alright, let's split this.",
rx=/\W*([a-zA-Z][a-zA-Z']*)(\W+|$)/g, words;

while((words= rx.exec(str))!= null){
    A.push(words[1]);
}
A.join(', ')

/*  returned value: (String)
This, is, a, long, string, Alright, let's, split, this
*/
于 2011-12-09T07:00:40.690 に答える
1
var words = y.split(/[^A-Za-z0-9]+/);
于 2011-12-09T06:49:01.537 に答える
1

これは、正規表現グループを使用して、さまざまな種類のトークンを使用してテキストをトークン化するソリューションです。

ここでコードをテストできますhttps://jsfiddle.net/u3mvca6q/5/

/*
Basic Regex explanation:
/                   Regex start
(\w+)               First group, words     \w means ASCII letter with \w     + means 1 or more letters
|                   or
(,|!)               Second group, punctuation
|                   or
(\s)                Third group, white spaces
/                   Regex end
g                   "global", enables looping over the string to capture one element at a time

Regex result:
result[0] : default group : any match
result[1] : group1 : words
result[2] : group2 : punctuation , !
result[3] : group3 : whitespace
*/
var basicRegex = /(\w+)|(,|!)|(\s)/g;

/*
Advanced Regex explanation:
[a-zA-Z\u0080-\u00FF] instead of \w     Supports some Unicode letters instead of ASCII letters only. Find Unicode ranges here https://apps.timwhitlock.info/js/regex

(\.\.\.|\.|,|!|\?)                      Identify ellipsis (...) and points as separate entities

You can improve it by adding ranges for special punctuation and so on
*/
var advancedRegex = /([a-zA-Z\u0080-\u00FF]+)|(\.\.\.|\.|,|!|\?)|(\s)/g;

var basicString = "Hello, this is a random message!";
var advancedString = "Et en français ? Avec des caractères spéciaux ... With one point at the end.";

console.log("------------------");
var result = null;
do {
    result = basicRegex.exec(basicString)
    console.log(result);
} while(result != null)

console.log("------------------");
var result = null;
do {
    result = advancedRegex.exec(advancedString)
    console.log(result);
} while(result != null)

/*
Output:
Array [ "Hello",        "Hello",        undefined,  undefined ]
Array [ ",",            undefined,      ",",        undefined ]
Array [ " ",            undefined,      undefined,  " "       ]
Array [ "this",         "this",         undefined,  undefined ]
Array [ " ",            undefined,      undefined,  " "       ]
Array [ "is",           "is",           undefined,  undefined ]
Array [ " ",            undefined,      undefined,  " "       ]
Array [ "a",            "a",            undefined,  undefined ]
Array [ " ",            undefined,      undefined,  " "       ]
Array [ "random",       "random",       undefined,  undefined ]
Array [ " ",            undefined,      undefined,  " "       ]
Array [ "message",      "message",      undefined,  undefined ]
Array [ "!",            undefined,      "!",        undefined ]
null
*/
于 2017-11-30T02:47:55.660 に答える
0

単語のみの文字を抽出するために、\w記号を使用します。これが Unicode 文字に一致するかどうかは実装に依存します。この参照を使用して、言語/ライブラリの場合を確認できます。

これを式に適用する方法については、Alexander Yezutov の回答 (更新 2) を参照してください。

于 2012-06-24T09:57:08.680 に答える