2

This is my text string:

0000> hello world <0000

I want to count the characters between "0000>" and "<0000".

nicholaswmin
  • 21,686
  • 15
  • 91
  • 167
  • 1
    Just for the record, if you are trying to parse an xml you shoud use a parser, [not regex](http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags). – Chango Nov 10 '12 at 21:42

5 Answers5

2

This will work:

s = "0000> hello my name is james, whats yours? <0000";
s.match(/0000>(.*?)<0000/)[1].length // returns 38;

But then again, so will this :-)

s.length - 10; // returns 38
AHM
  • 5,145
  • 34
  • 37
1

Well, something like this would count everything (including spaces) between 0000> and <0000:

'0000> hello my name is james, whats yours? <0000'
  .split(/0000>|<0000/g)[1].length; //=> 38

Or

'0000> hello my name is james, whats yours? <0000'
     .replace(/0000>|<0000/g,'').length; //=> 38
KooiInc
  • 119,216
  • 31
  • 141
  • 177
  • you don't need the last split(''). Strings have a length property of their own. – AHM Nov 10 '12 at 13:02
1

This will do:

function count(string) {
  var match = /0000>(.*)<0000/g.exec(string);

  if (match.length > 1) {
    return match[1].trim().length;
  } else {
    return null;
  }
}

alert (count("0000> hello my name is james, whats yours? <0000"));

And the jsfiddle demo: http://jsfiddle.net/pSJGk/1/

Chango
  • 6,754
  • 1
  • 28
  • 37
  • unfortunately i still have a problem. this is my string: Layer 1 really? i want to perform the calculations on this string and it doesnt seem to work. – nicholaswmin Nov 10 '12 at 17:57
  • the word i want to count in this example is the work "really?". Its between the words "none"> and – nicholaswmin Nov 10 '12 at 18:28
  • If you want to parse xml use a parser, [not regex](http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags) – Chango Nov 10 '12 at 21:37
0

I'd suggest:

var str = " 0000> hello my name is james, whats yours? <0000",
    start = "0000>",
    end = "<0000",
    between = str.substring(str.indexOf(start) + start.length, str.indexOf(end)).length;

console.log(between);​

JS Fiddle demo.

This doesn't, however, trim the white-space from after the first match, or from before the second. This could be altered, of course, to match any given string delimiters by simply changing the variables.

References:

David Thomas
  • 249,100
  • 51
  • 377
  • 410
0
function getLengthBetween(str,startStr,stopStr) {
  var startPos = str.indexOf(startStr);
  if(startPos == -1) {
    return 0;
  }
  var startOffset = startPos+startStr.length;
  var stopPos  = str.indexOf(stopStr,startOffset);
  if(stopPos == -1) {
    stopPos = str.length;
  }
  return stopPos-startOffset;
}

Usage:

getLengthBetween("0000> hello my name is james, whats yours? <0000","0000>","<0000");
dda
  • 6,030
  • 2
  • 25
  • 34
lostsource
  • 21,070
  • 8
  • 66
  • 88
  • unfortunately i still have a problem. this is my string: ; Layer 1 really? i want to perform the calculations on this string and it doesnt seem to work. – nicholaswmin Nov 10 '12 at 18:02
  • well I dont see the original 'strings' "0000>" and "<0000".. which text are you trying to find the length of in this string? – lostsource Nov 10 '12 at 18:10
  • actually that was an example. the original strings are fill="none"> and respectively. its at the end of the string i just posted. in this example i need to count the characters of the word "really?". – nicholaswmin Nov 10 '12 at 18:18
  • its an xml output i think. does that make it any different? – nicholaswmin Nov 10 '12 at 18:22
  • assuming that string is assigned to variable 'myString' .. i tried getLengthBetween(myString,'fill="none">','') and it gave me 7 .. as long as its of type 'string' it should work (even though it looks like xml) .. can you do an alert(typeof(myString)) ? it should say 'string' .. otherwise none of the answers here would work – lostsource Nov 10 '12 at 18:33
  • unfortunately i am just now starting getting into javascript? can you do a jsfiddle demo???? that will help me a lot!! only if you have time. thanks for all the answers anyway. – nicholaswmin Nov 10 '12 at 18:39
  • it really depends where that string is coming from.. are you seeing it in a console.log ? .. it is possible that this is not a string value afterall.. try adding alert(typeof(str)) on the first line of my function – lostsource Nov 10 '12 at 18:44
  • nicholas.kyriakides@hotmail.com – nicholaswmin Nov 10 '12 at 18:47
  • @NikolasKyriakides check your inbox – lostsource Nov 10 '12 at 20:42