0

While parsing data from an http response, I ran across a special character '\u21b5' that looks like , is there a method that will detect this in Angular?

This is what my data looks like,

["Title", "First", "Last", "Email", "↵" ,"Title", "First", "Last", "Email", "↵" ,"Title", "First", "Last", "Email"]

CGundlach
  • 671
  • 4
  • 13
EvOlaNdLuPiZ
  • 600
  • 1
  • 4
  • 21

1 Answers1

1

Regular indexOf only takes a string parameter, no regular expressions.

const arr = ["Title", "First", "Last", "Email", "↵"];
arr.indexOf('\u21b5'); // 4

To use regular expressions, you habe to use the find method. Note that for Java-/Typescript to parse the unicode character in a regular expression, you need to use the u flag - See also this, taken from this answer.

const char = arr.find(item => /\u{21b5}/u.test(item));
if (char) {
    result = arr.indexOf(char); // 4
}

You can also write a custom RegEx-enabled indexOf function:

function regExIndexOf(array: string[], term: RegExp, start = 0): number {
  let result = -1;
  array.forEach((item, index) => {
    if (result === -1 && index >= start && term.test(item)) {
      result = index;
    }
  });
  return result;
}

regExIndexOf(["Title", "First", "Last", "Email", "↵"], new RegExp('Title')); // 0
regExIndexOf(["Title", "First", "Last", "Email", "↵"], new RegExp('Title'), 1); // -1
regExIndexOf(["Title", "First", "Last", "Email", "↵"], new RegExp('\u{21b5}', 'u')); //4

See on Typescript Playground

CGundlach
  • 671
  • 4
  • 13
  • the routine is making "term" parameter print /arrow/ and it changes by modifying '\u21b5' only proble is it still has these two arrows in the print /arrow/ – EvOlaNdLuPiZ Aug 21 '19 at 07:43
  • it's the RegExp function causing those forward slashes. – EvOlaNdLuPiZ Aug 21 '19 at 07:45
  • I can't understand what exactly you mean with your second comment. Can you paste the code you used and the result? I've added a Typescript playground link to my answer that shows that this should work, you can also test there. – CGundlach Aug 21 '19 at 08:13
  • There was a small bug in the function that made it always return the index of the last matched item in the array, not the first. I've updated it to skip looking after a match has been found. It's probably still slower and can be optimized by using a for...of loop and the array.entries() function, but the version in the answer is still a good and legible start to understand this, imho. – CGundlach Aug 21 '19 at 08:27
  • i continue to test with my data that i provided and the code you provided, i still get -1, unfortunately. i think it's having to do something wiht 'unicode 21b5' i'm developing on osx. – EvOlaNdLuPiZ Aug 21 '19 at 18:51
  • `if (e.charCodeAt(0) === 10) { console.log( 'found one' ); }` , this solution let's me at least determine that it's a LF (line feed), so this is one way to work it out :) – EvOlaNdLuPiZ Aug 21 '19 at 18:58
  • while your answer didn't necessarily solve my problem, it lead to the solution i mentioned above ^ please put that on your post and i'll mark it as an answer. – EvOlaNdLuPiZ Aug 21 '19 at 18:59
  • ... The special character `\u21B5` - is that actually in the data or does it only show up in your notepad (or osx-equivalent)? The char with the char code `10` is a linefeed. It's unicode character escape sequence is `\u000A`. It's also often written as `\n`. So if you use `\n` or `\u{000A}` as the search term, it should work as designed. – CGundlach Aug 22 '19 at 09:31
  • To answer your question, it is actually in the data. Thank you – EvOlaNdLuPiZ Aug 22 '19 at 14:50