Convert string.match() result to string?

Asked

Viewed 84 times

3

How can I convert the return method match() string? For example:

var value:string = ">>>> Ola"
var result:string = value.match(/\w+/g)

The above example gives error in variable result. I’ve tried to put it that way with .toString(), but it doesn’t work:

var result:string = value.match(/\w+/g).toString()

How can I take the value of regex and turn it into a string?

2 answers

1

The return of the match() function is an array, so it can be converted to string as follows:

var value = ">>>> Ola"
var result = value.match(/\w+/g) !== null
    ? value.match(/\w+/g).join(' ') 
    : '';

alert(result);

Thus, the result would concatenate all matchs with a space, if there is a need to replace the space with some character, just change the text inside the Join function.

var value = ">>>> Ola >>>>> teste"
var result = value.match(/\w+/g) !== null
    ? value.match(/\w+/g).join('|||') 
    : '';

alert(result);

  • 1

    Is giving error 'Object is possibly 'null'. Only working in Javascript

  • I’ll update the reply

  • The Object is possibly 'null' problem occurred because the return of the match function could be null, I made a ternary IF so that when null, assign '' to the string

1


How did you use the flag g, the method match returns an array containing all parts found (except capture groups, but since your regex doesn’t have that, then okay).

So at the bottom, it all comes down to turning an array into a string. And then it depends, there are several ways to do it.

In your specific case, if you "know" that you will only have one occurrence, you can simply take the first element of the array:

let value = ">>>> Ola";
let result = value.match(/\w+/g);
if (result) {
    console.log(result[0]);
}

I check whether result is valid, because when no one is found match, the method match returns null (and in that case, there’s nothing to print).


If there is the possibility of returning more than one match, then just go through the array and get the strings one by one:

let value = ">>>> Ola, mundo";
let result = value.match(/\w+/g);
if (result) {
    result.forEach(s => console.log(s));
}

Or still use join, for example, to show all strings concatenated at once:

let value = ">>>> Ola, mundo";
let result = value.match(/\w+/g);
if (result) {
    console.log(result.join(', '));
}

Anyway, once you have the array with the strings found, you decide the best way to show them.

  • Hello, can I only get the string in the output? For example via console? Can you declare a variable with the value of regex?

  • @Isaac Seria that let valor = result.join(', ')? Thus, valor is a string and you can do whatever you want with it. I don’t know if I got it right what you want...

  • That’s right, but Typescript doesn’t have the . Join() method in match(), correct me if I’m wrong

  • @Isaac Ué, match returns what then, is not an array? I just tested it here and it worked...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.