How to replace characters or words in a string using javascript even with no space between them?

javascript remove spaces between words
remove extra space between words javascript
javascript remove spaces from string
javascript replace
remove space in middle of string javascript
remove spaces from array javascript
javascript remove part of string
remove all whitespace from string

I have a function something like this :

 var exchange = function(code) {
     var ref = {
       'one' : '1' , 
       'two' : '2' ,
       'three' : '3'
     }; 
   return code.split(' ').map( function (a) { 
     return a.split(' ').map( function (b) { 
        return ref[b]; 
     }).join(''); 
   }).join(' '); 
 };

So now I do it :

 var strg = "one two three";
 alert( exchange( strg ) ); // => 123

It works fine but I have a problem. Let me explain. I want it to execute same as now till with no space between.

For example :

 var strg = "onetwothree";
 alert( exchange( strg ) ); // => Nothing

But I want it to change the text even with no spaces. How can I do that ?


You could create a regular expression with a joined string with pipe and replace found string with the values.

var exchange = function(code) {
        var ref = { one: '1',  two: '2', three: '3' }; 
        return code.replace(new RegExp(Object.keys(ref).join('|'), 'ig'), k => ref[k]);
    };

console.log(exchange("onetwothree"));

Replace Character in a String in Java without using replace() method, How do you replace a character in a string in Java without using replace method? If you're searching and replacing through a string with a static search and a static replace it's faster to perform the action with.split ("match").join ("replace") - which seems counter-intuitive but it manages to work that way in most modern browsers.


You can iterate over number values and replace them.

 var exchange = function(code) {
     var ref = {
       'one' : '1' , 
       'two' : '2' ,
       'three' : '3'
     }; 
   Object.keys(ref).forEach(k => code = code.replace(k, ref[k]))
  return code
 };
 var strg = "onetwothree";
 alert( exchange( strg ) ); // => 123

URLify a given string (Replace spaces is %20), res = str. substring(0, pos) + rep + str. The \s meta character in JavaScript regular expressions matches any whitespace character: spaces, tabs, newlines and Unicode spaces. And the g flag tells JavaScript to replace it multiple times. If you miss it, it will only replace the first occurrence of the white space. Remember that the name value does not change. So you need to assign it to


I think likely the best thing to do is to create a standard parser constructor that goes character by character and then provide it a configuration and a function wrapper.

Constructor

//Parsing input output constructor;
function ParseFor( i, o ) {
    this.str = i;
    this.indexLength = this.str.length - 1;
    this.arr = [ ...i ];
    this.pointer = 0;
    this.output = o;
    this.match = false;
    this.reset = function() {
        this.pointer = 0;
        this.match = false;
    }
    this.next = function( c ) {
        if ( this.arr[ this.pointer ] === c ) {
            if ( this.pointer === this.indexLength ) {
                this.match = true;
            } else {
                this.pointer++;
            }
        } else {
            this.reset();
        }
        return this;
    }
}

Parse Work with RegEx and Constructor

    let parsers = [];
    for ( let [ k, v ] of Object.entries( ref ) ) parsers.push( new ParseFor( k, v ) );
    let parsedStr = str.replace( /./g, ( c ) => {
        let output = "";
        parsers = parsers.map( parser => parser.next( c ) );
        parsers.forEach( parser => {
            if ( parser.match ) {
                parser.reset();
                output = parser.output;
            }
        } );
        return output;
    } );

Parse Function Wrapper

function parseStr( str, ref ) {

//... PARSE CONSTRUCTOR

//... PARSE WORK
    return parsedStr;
}

parseStr( "onetwothree",  {
        'one': '1',
        'two': '2',
        'three': '3'
    });

EXAMPLE:

function parseStr( str, ref ) {

//Parsing input output constructor;
function ParseFor( i, o ) {
	this.str = i;
	this.indexLength = this.str.length - 1;
	this.arr = [ ...i ];
	this.pointer = 0;
	this.output = o;
	this.match = false;
	this.reset = function() {
		this.pointer = 0;
		this.match = false;
	}
	this.next = function( c ) {
		if ( this.arr[ this.pointer ] === c ) {
			if ( this.pointer === this.indexLength ) {
				this.match = true;
			} else {
				this.pointer++;
			}
		} else {
			this.reset();
		}
		return this;
	}
}

//parse work

	let parsers = [];
	for ( let [ k, v ] of Object.entries( ref ) ) parsers.push( new ParseFor( k, v ) );
	let parsedStr = str.replace( /./g, ( c ) => {
		let output = "";
		parsers = parsers.map( parser => parser.next( c ) );
		parsers.forEach( parser => {
			if ( parser.match ) {
				parser.reset();
				output = parser.output;
			}
		} );
		return output;
	} );
	return parsedStr;
}

console.log( parseStr( "onetwothree",  {
		'one': '1',
		'two': '2',
		'three': '3'
	}) );

How to replace multiple spaces in a string using a single space , How do you replace each given character to other eg blank with 20? splitString[1]; Output. are. If an empty parameter is given, split () will create a comma-separated array with each character in the string. By splitting strings you can determine how many words are in a sentence, and use the method as a way to determine people’s first names and last names, for example.


How to replace characters or words in a string using javascript even , You could create a regular expression with a joined string with pipe and replace found string with the values. var exchange = function(code)  If we need positions of further matches, we should use other means, such as finding them all with str.matchAll(regexp). str.replace(str|regexp, str|func) This is a generic method for searching and replacing, one of most useful ones. The swiss army knife for searching and replacing. We can use it without regexps, to search and replace a substring:


Regular Expressions Cookbook, JavaScript programmers cannot use the first version, because JavaScript You can implement these regular expressions by following the code in Recipe 3.6. You also want to allow differing amounts of whitespace between words, even if this first word but remove subsequent duplicate words, replace all matches with​  If for some reason you only wanted the first found occurrence in the string to be replaced then you would not use the "g". Improving Javascript Code for Line Break Removal. In many real world uses of the javascript code it's probably better to replace the line breaks with a space instead of just removing them entirely.


JavaScript: The Missing Manual: The Missing Manual, How do I know when I'm supposed to use space characters, and when I'm not The JavaScript interpreter needs the space between var and score to identify the need a space on either side of an assignment operator, but you can add them if carriage return inside a string; in other words you can't split a string over two  Well, String class in Java provides several methods to replace characters, CharSequence and substring from a String in Java.You can call replace method on the String, where you want to replace characters and it will return a result where characters are replaced.