2

I a string name content that has inside the text "data-RowKey=xxx". I am trying to get out xxx so I tried the following:

var val = content.substring(12 + content.indexOf("data-RowKey="), 3);

This does not work at all. rather than just get three characters I get a very long string. Can anyone see what I am doing wrong

1
  • If you provide an example of the content, we might be able to offer better approaches. Commented May 31, 2012 at 16:40

4 Answers 4

2

You're using wrong tool. When you want to capture a data matching some pattern, you should use regular expressions. If your value is exactly three symbols, correct expression would be /data-RowKey=(...)/ with . standing for any symbol and () specifying part to capture.

Sign up to request clarification or add additional context in comments.

4 Comments

If the "pattern" is static, it's absolutely ok to not use regular expressions. - is only a special character inside character classes.
Indeed. Fixed. Sorry, but calling two separate object methods instead of built-in facility specifically written to parse patterns is "not ok" in my book. Operating directly on string indexes is no better than manually assigning memory - you'll have to track everything yourself. In this case, for example it will fail with exception if there's no "data-RowKey=".
Regular expression are more expensive than simple string processing methods. See jsperf.com/string-methods-vs-regex
It's not always obvious what's just string and what's regEx. A lot of string methods like replace and split also take regEx literals and probably do the work via regEx functionality under the hood. RegEx is powerful, but not necessarily slow if optimized. The '.' wildcard for instance is one of the most expensive yet almost always unnecessary things you can use in RegEx.
2

.substring() [MDN] takes two indexes, .substr() [MDN] takes an index and the length. Try:

var val = content.substr(12 + content.indexOf("data-RowKey="), 3);

If "data-RowKey=xxx" is the whole string, there are various other ways to get xxx:

var val = content.replace('data-RowKey=', '');
var val = content.split('=')[1]; // assuming `=` does not appear in xxx

Comments

1

This works:

var value = content.match(/data-RowKey=(.*)/)[1];

Live DEMO

If there could be values after the xxx, use this:

"data-RowKey=123abc".match(/data-RowKey=(.{3}).*/)[1] // 123

4 Comments

That would also match everything that comes after data-RowKey=xxx.
@FelixKling. In his code, there's not. But I added a way for that.
There are only three characters after the RowKey=. Will it match just the three?
@Gemma. Yes it will, you can try it yourself with the DEMO I linked.
0

If your rowkey is numeric, this might be best since you get the number as an integer and wouldn't need to convert later:

var val = parseInt( content.split("data-RowKey=")[1] );

If always the three characters and/or no need to convert:

var val = content.split("data-RowKey=")[1].substring(0,3);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.