EN
TypeScript - find text in string
0 points
In TypeScript it is possible to find text in string with String
search
method.
String
search
method works in following way:
- as first argument takes regular expression
- returns first text occurrance position,
- returns
-1
value if nothing found
xxxxxxxxxx
1
const text: string = 'This is my text...';
2
3
const position1: number = text.search('is'); // be careful because of regex characters
4
const position2: number = text.search('\\bis\\b');
5
6
console.log(position1); // 2
7
console.log(position2); // 5
xxxxxxxxxx
1
const text: string = 'This is my text...';
2
const position: number = text.search(/\bis\b/);
3
4
console.log(position); // 5