私はこのひもを持っています
'john smith~123 Street~Apt 4~New York~NY~12345'JavaScriptを使用して、これを解析する最も速い方法は何ですか
var name = "john smith";
var street= "123 Street";
//etc...
私はこのひもを持っています
'john smith~123 Street~Apt 4~New York~NY~12345'JavaScriptを使用して、これを解析する最も速い方法は何ですか
var name = "john smith";
var street= "123 Street";
//etc...
回答:
JavaScriptのString.prototype.split機能を使用する場合:
var input = 'john smith~123 Street~Apt 4~New York~NY~12345';
var fields = input.split('~');
var name = fields[0];
var street = fields[1];
// etc.
ECMAScript6 ES6によると、きれいな方法は配列を分解することです:
const input = 'john smith~123 Street~Apt 4~New York~NY~12345';
const [name, street, unit, city, state, zip] = input.split('~');
console.log(name); // john smith
console.log(street); // 123 Street
console.log(unit); // Apt 4
console.log(city); // New York
console.log(state); // NY
console.log(zip); // 12345入力文字列に余分な項目がある可能性があります。この場合、rest演算子を使用して、残りの配列を取得するか、単に無視することができます。
const input = 'john smith~123 Street~Apt 4~New York~NY~12345';
const [name, street, ...others] = input.split('~');
console.log(name); // john smith
console.log(street); // 123 Street
console.log(others); // ["Apt 4", "New York", "NY", "12345"]値の読み取り専用参照を想定して、const宣言を使用しました。
ES6をお楽しみください!
const [name, , unit, ...others] = ...
                    これは最も簡単な方法ではありませんが、これを行うことができます:
var addressString = "~john smith~123 Street~Apt 4~New York~NY~12345~",
    keys = "name address1 address2 city state zipcode".split(" "),
    address = {};
// clean up the string with the first replace
// "abuse" the second replace to map the keys to the matches
addressString.replace(/^~|~$/g).replace(/[^~]+/g, function(match){
    address[ keys.unshift() ] = match;
});
// address will contain the mapped result
address = {
    address1: "123 Street"
    address2: "Apt 4"
    city: "New York"
    name: "john smith"
    state: "NY"
    zipcode: "12345"
}破壊を使用したES2015の更新
const [address1, address2, city, name, state, zipcode] = addressString.match(/[^~]+/g);
// The variables defined above now contain the appropriate information:
console.log(address1, address2, city, name, state, zipcode);
// -> john smith 123 Street Apt 4 New York NY 12345keysます。2番目の置換関数は[^~]+、さまざまな部分(「123 Street」、「Apt 4」など)を照合するために使用し、各部分の関数を呼び出して、引数として渡します。各実行時に、関数はキー配列から最初のキーを取得し(これもArray.unshiftを使用して削除します)、キーとパーツをアドレスオブジェクトに割り当てます。
                    何かのようなもの:
var divided = str.split("/~/");
var name=divided[0];
var street = divided[1];おそらく最も簡単になるでしょう
split("~")かsplit(/~/)が必要ですが、必要はありませんsplit("/~/")。後者は分割されるだけで、分割さ"John/~/Smith"れません"John~Smith"。
                    を使用splitしてテキストを分割できます。
別の方法として、match次のように使用することもできます
var str = 'john smith~123 Street~Apt 4~New York~NY~12345';
matches = str.match(/[^~]+/g);
console.log(matches);
document.write(matches);正規表現[^~]+は、以外のすべての文字~に一致し、一致したものを配列で返します。その後、そこから一致を抽出できます。
str.split();Firefoxでは機能しませんでしたが、これはChromeとFirefoxの両方で機能しました。
                    str.split(); Firefoxとすべての主要ブラウザで動作します。
                    ザックはこれを正しく処理しました。彼の方法を使用して、一見「多次元」の配列を作成することもできます。JSFiddleで簡単な例を作成しましたhttp://jsfiddle.net/LcnvJ/2/
// array[0][0] will produce brian
// array[0][1] will produce james
// array[1][0] will produce kevin
// array[1][1] will produce haley
var array = [];
    array[0] = "brian,james,doug".split(",");
    array[1] = "kevin,haley,steph".split(",");JavaScript:文字列を配列に変換JavaScript分割
    var str = "This-javascript-tutorial-string-split-method-examples-tutsmake."
 
    var result = str.split('-'); 
     
    console.log(result);
     
    document.getElementById("show").innerHTML = result; <html>
<head>
<title>How do you split a string, breaking at a particular character in javascript?</title>
</head>
<body>
 
<p id="show"></p> 
 
</body>
</html>https://www.tutsmake.com/javascript-convert-string-to-array-javascript/
これstring.split("~")[0];は物事を成し遂げる。
カレーと関数の構成を使用した別の機能的アプローチ。
つまり、最初の機能はsplit関数です。これ"john smith~123 Street~Apt 4~New York~NY~12345"をこれにしたい["john smith", "123 Street", "Apt 4", "New York", "NY", "12345"]
const split = (separator) => (text) => text.split(separator);
const splitByTilde = split('~');これで、専用のsplitByTilde関数を使用できます。例:
splitByTilde("john smith~123 Street~Apt 4~New York~NY~12345") // ["john smith", "123 Street", "Apt 4", "New York", "NY", "12345"]最初の要素を取得するには、list[0]演算子を使用できます。first関数を作成しましょう:
const first = (list) => list[0];アルゴリズムは、コロンで分割され、指定されたリストの最初の要素を取得します。これらの関数を作成して、最終的getNameな関数を作成できます。でcompose関数を作成するreduce:
const compose = (...fns) => (value) => fns.reduceRight((acc, fn) => fn(acc), value);そして今、それを使って作曲しsplitByTilde、first機能しています。
const getName = compose(first, splitByTilde);
let string = 'john smith~123 Street~Apt 4~New York~NY~12345';
getName(string); // "john smith"コンマでの分割の質問はこの質問に複製されるため、ここに追加します。
文字で分割し、その文字に続く可能性のある余分な空白も処理したい場合は、コンマでよく発生しますが、次のようにreplacethen を使用できますsplit。
var items = string.replace(/,\s+/, ",").split(',')このコードを使用-
function myFunction() {
var str = "How are you doing today?";
var res = str.split("/");
}