2011-10-05 3 views
0

は、例えば、文字列です:文字列の特定の部分はどのようにして得られますか?ここ

survey_questions_attributes_1317784471568_answers_attributes 

私は、文字列の1317784471568アウトを取得したいです。

しかし、その部分もテキストである可能性があります。たとえば、文字列の同じ場所からnew_questionsという文字列を取得する必要があるかもしれません。

ここでの定数は、常にsurvey_questions_attributes_が常に私が望むチャンクの前にあり、_answers_attributesが常にそれに続くということです。

答えて

2
id = str.match(/_questions_attributes_(.+)_answers_attributes/)[1]; 
2
var str = "survey_questions_attributes_1317784471568_answers_attributes"; 

var newStr = str.replace("survey_questions_attributes_", ""); 
newStr = newStr.replace("_answers_attributes", ""); 

正規表現などを使用せずに考えるのが最も簡単な方法です。

1

TRY -

var text = "survey_questions_attributes_1317784471568_answers_attributes" 
     .replace("survey_questions_attributes_","") 
     .replace("_answers_attributes","") 
-1

サブストリングを使用する():

mystring = "survey_questions_attributes_1317784471568_answers_attributes" 
newstring = mystring.substring(28, 41) 
+0

間違った仮定かもしれ一定の長さ、であることが判明する事が必要です。 – Joe

+0

Joeが正しいです。文字列は一定の長さではありません。 – Shpigford

+0

元の質問は、数字を囲む文字列が「定数」であることを示しています。 –

関連する問題