2016-09-21 22 views
0

特殊文字の後に最初の文字を見つけようとしています。たとえば : 文字列:犬//99 11は、私はjQueryの1(最初の後に文字/)、その後、9見つけたい(第2の後に文字を/)文字の後に最初の文字列を見つける

<table border="1"> 
    <tr><td class="abc">apple</td></tr> 
    <tr><td class="abc">ball</td></tr> 
    <tr><td class="abc">cat/55/77</td></tr> 
    <tr><td class="abc">dog/11/99</td></tr> 
</table> 

私は目を通す必要があります全体のテーブルとその後、私に特殊文字の後の文字を与える。

どのように私はそれを達成することができますか?

+0

ネイティブJSで利用可能な正規表現を使用してください。 – evolutionxbox

+2

'string.split( '/')。スライス(1).map(x => x [0])' – adeneo

+0

@adeneoまた、最初の文字を返します – Phil

答えて

1

は、正規表現を使用してみてください:

/\/(.)/g 

任意の/後の最初の文字を返すこと。たとえば
match[1]

var match, regex = /\/(.)/g; 
while (match = regex.exec('dog/11/99')) console.log(match); 
// ["/1", "1", index: 3, input: "dog/11/99"] 
// ["/9", "9", index: 6, input: "dog/11/99"] 

は、ご希望の文字であること。

0

あなたは、文字列のインデックスで文字を選択し取得する単語文字、返される配列、String.prototype.slice()、ブラケット表記の.indexない文字に一致するようにRegExp/\W/.match()を使用することができます。

var str = document.querySelector("table tr:last-child td").textContent; 
 
var index = str.match(/\W/).index; 
 
var res1 = str[index + 1]; 
 
var res2 = str.slice(index, str.length).match(/\W/).index; 
 
var res2 = str[ 
 
      str.slice(index + 1, str.length).match(/\W/).index + 1 
 
      + index + 1 
 
      ]; 
 

 
console.log(res1, res2);
<table border="1"> 
 
    <tr><td class="abc">apple</td></tr> 
 
    <tr><td class="abc">ball</td></tr> 
 
    <tr><td class="abc">cat/55/77</td></tr> 
 
    <tr><td class="abc">dog/11/99</td></tr> 
 
</table>

関連する問題