user3791078
user3791078

Reputation: 71

String manipulation using javascript

I am working on some string manipulations using javascript.I have a senario where i need to do a search in the string and remove certain words.

Here is my senario: When i click 'Click me' it should look for the word in the input from the string variable, and if matching found it should remove that word from the input.

Here is my sepecial senario, while removing the word from the input, it should remove the : and the integer value and comma (if available) which is there before the matching word.

In my example input is 1:first user,2:second user in the text box i need the output as 2:second user

How can i achive this

 <input id='textinput' type='text' value='1:first user,2:second user'></input>
 <div class='click'>Click me</div>
$(document).ready(function () {
    $(".click").click(function () {
        var string = 'first user';
        $('#textinput').val().replace(/string/g, '');
    });
});

i have created a fiddle http://jsfiddle.net/h9D5W/

EDIT

here my variable string contains only user names i can't append id's with user names for example 1:first user, so the exact match will not be there in the string variable.

Upvotes: 0

Views: 93

Answers (5)

michikot
michikot

Reputation: 142

I have updated your fiddle

first, i split your string into array

var inputArr = ($('#textinput').val()).split(',');

so i can check each set of words using for loop,

    for (i = 0; i < inputArr.length; i++) {

        if (inputArr[i].indexOf(string) == -1) {
            newStr = newStr + inputArr[i] + ',';
        }
    }

then i set the new value using substring to eliminate the last comma appended.

$('#textinput').val(newStr.substring(0, newStr.length - 1));

Upvotes: 0

Nabeel Bape
Nabeel Bape

Reputation: 60

I have removed the starting digits, underscore and ending comma.

$(document).ready(function () {

    $(".click").click(function () {
        var string = 'first user';
        var re = new RegExp('\\d:*'+string+',*',"g");
        var text = $('#textinput').val().replace(re, '');;
        $('#textinput').val(text);

    });
});

Check the demo

http://jsfiddle.net/yKfur/1/

Upvotes: 3

Satpal
Satpal

Reputation: 133403

You should use RegExp, You need to initialize it with string variable.

Use

$(".click").click(function () {
    var string = 'first user';
    var re = new RegExp(string,"g"); //initialize RegExp
    var text = $('#textinput').val().replace(re, ''); //Get replaced text
    $('#textinput').val(text);  //Reset its value      
});

DEMO

EDIT

here i cant use 1:first user in the variable

$(".click").click(function () {
    var arr = $('#textinput').val().split(','); //Here used split method to split string ',' as deliminator 
    $('#textinput').val(arr[arr.length - 1]);        
});

DEMO

Upvotes: 1

RobG
RobG

Reputation: 147363

You can use a regular expression like:

 var text = '1:first user,2:second user,3:third user';

 var s = 'first user';

 var re = new RegExp('^\\d+:' + s + ',?|,\\d+:' + s + '\\b');

 text.replace(re, '') // '2:second user,3:third user'

I'm sure there's a shorter one though.

Upvotes: 0

hjpotter92
hjpotter92

Reputation: 80639

In your JavaScript code, string is a variable. You'd need to initiate the RegExp class to generate the pattern:

Also, please refrain from using variable name like string. It's the name of a global object.

var s = 'first user';
var x = $( '#textinput' ).val().replace(new RegExp(s, 'g'), '');
// use the new variable x as you wish

Upvotes: 1

Related Questions