質問

私はこの文字列

'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.

他のヒント

必要なjQuery.

var s = 'john smith~123 Street~Apt 4~New York~NY~12345';
var fields = s.split(/~/);
var name = fields[0];
var street = fields[1];

に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

い項目の入力文字列になります。この場合、利用できる憩いのオペレータ配列を取得し、残りは無視して

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!

でもこんなに単純な、なにができること:

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、destructuring

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 12345

を見ると良いでしょうへJavaScriptの substr または 分割, という作業に適したjQuery.

でも最も簡単な方法のようなものです:

var address = theEncodedString.split(/~/)
var name = address[0], street = address[1]

の場合 Spliterは見つかり その

してもらえるのでしょうか

他に戻る 同じ文字列

function SplitTheString(ResultStr) {
    if (ResultStr != null) {
        var SplitChars = '~';
        if (ResultStr.indexOf(SplitChars) >= 0) {
            var DtlStr = ResultStr.split(SplitChars);
            var name  = DtlStr[0];
            var street = DtlStr[1];
        }
    }
}

のようなもの:

var divided = str.split("/~/");
var name=divided[0];
var street = divided[1];

でしょうるのが一番簡単

利用できる split 分割を行います。

の代替としても利用できます match してい

var str = 'john smith~123 Street~Apt 4~New York~NY~12345';
matches = str.match(/[^~]+/g);

console.log(matches);
document.write(matches);

のregex [^~]+ した場合、全ての文字を除く ~ 返しに一致する配列の型になります。でき抽出し、試合からです。

ザックがここに。を用いた方法はできるだけではなく、一見"多次元の"配列..では迅速例で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(",");

この string.split("~")[0]; 得も行います。

ソース: 文字列になります。試作品です。(株)


他の機能を使ったアプローチのカレーおよび機能構成です。

で最初の分割機能です。としたい "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);

現在で構成 splitByTildefirst ます。

const getName = compose(first, splitByTilde);

let string = 'john smith~123 Street~Apt 4~New York~NY~12345';
getName(string); // "john smith"

以来、分割にコンマの問題は複製されたこの問いを追加することによります。

したい場合は分割文字とともに余白がこれに従って文字を、多くの場合をカンマ区切りで入力でき replace その split, このよう:

var items = string.replace(/,\s+/, ",").split(',')

み平Javascript

 //basic url=http://localhost:58227/ExternalApproval.html?Status=1

 var ar= [url,statu] = window.location.href.split("=");

このコードを--

function myFunction() {
var str = "How are you doing today?";
var res = str.split("/");

}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top