skolind
skolind

Reputation: 1754

How can I find a specific string and replace based on character?

I'm trying to find a specific character, for example '?' and then remove all text behind the char until I hit a whitespace.

So that:

var string = '?What is going on here?';
Then the new string would be: 'is going on here';

I have been using this:

var mod_content = content.substring(content.indexOf(' ') + 1);

But this is not valid anymore, since the specific string also can be in the middle of a string also.

I haven't really tried anything but this. I have no idea at all how to do it.

Upvotes: 0

Views: 124

Answers (2)

xdazz
xdazz

Reputation: 160863

use:

string = string.replace(/\?\S*\s+/g, '');

Update: If want to remove the last ? too, then use

string = string.replace(/\?\S*\s*/g, '');

Upvotes: 2

DrLazer
DrLazer

Reputation: 3113

var firstBit = str.split("?");
var bityouWant = firstBit.substring(firstBit.indexOf(' ') + 1);

Upvotes: 0

Related Questions