I'm trying to check if a string has >= and then, if so, replace that with the ascii character for greater than and equal to. I'm working in Angular in the TS file and currently have:
@Input()
public set textLabel(value: string) {
let labelSymbols = value
// figure out how to check if string has >=
// if string has >=, replace with ASCII character
this._textLabel = labelSymbols
this._changeDetectorRef.detectChanges();
}
public get textLabel(): string {
return this._textLabel
}
private _textLabel: string;
What do I need to do to change the greater than and equal to when it occurs in the string?
CodePudding user response:
From the info I got from your comment, you are simply looking for a search and replace. You can use the replaceAll
function to do that.
function replaceAllGreaterOrEqualsChar(input) {
return input.replaceAll('>=', '≥');
}
const originalString = "this is >= a test, with >= multiple instances.";
// we pass the original string in our custom function,
const output = replaceAllGreaterOrEqualsChar(originalString);
// we print the results to the console.
console.log('output', output);
CodePudding user response:
You can use the regular expression, />=/g
to find every occurrence of >=
and replace it with ≥
, as shown in the below code snippet,
@Input()
public set textLabel(value: string) {
let regexToMatch = />=/g
this._textLabel = value.replace(regexToMatch, "≥");;
this._changeDetectorRef.detectChanges();
}
public get textLabel(): string {
return this._textLabel
}
private _textLabel: string;