如何在字符串中查找单词的部分出现并替换整个单词

时间:2022-12-27 19:21:39

I want to find the partial occurrence of a word in a string and attach a <span> around it.

我想在字符串中找到一个单词的部分出现并在其周围附加

More in detail, I want to find all the occurrences of words starting with the percentage sign "%" (for example %mystring) and replace them with <span>%mystring<span>.

更详细的说,我想找到所有以百分号“%”开头的单词(例如%mystring),并用%mystring 替换它们。

I found some similar solutions in PHP, but I'm not sure how to go about this in JavaScript.

我在PHP中找到了一些类似的解决方案,但我不确定如何在JavaScript中实现这一点。

4 个解决方案

#1


1  

try this:

尝试这个:

str = 'go to this %link not this %link';
replacedStr = str.replace(/\s\%(.*?)(\s|$)/g, ' <span>%$1</span>$2');

#2


0  

please try the next code:

请尝试下一个代码:

var str = "my str is %something %somethingElse";
var newStr = str.replace(/%(\S+)/g, function(match, p1) {return '<a>' + p1 + '</a>'});

#3


0  

if there is no mandate to use regex :), then try

如果没有使用正则表达式的授权:),那么试试吧

var text = "sadfg dfgikwer sfd dfg3 dfg4 sdfsdfb";
text = text.split( " " ).map( function(value){ if ( value.indexOf( "dfg" ) != 0 ){ return value }else { return "new" } } ).join( " " );
alert(text);

this replaces a word that begins with dfg with new

这将替换以dfg开头的单词

making this a method

使这个方法

function replaceOldWithNew( text, oldStr, newStr )
{
  return text = text.split( " " ).map( function(value){ if ( value.indexOf( oldStr ) != 0 ){ return value }else { return newStr } } ).join( " " );
}

 replaceOldWithNew( "sadfg dfgikwer sfd dfg3 dfg4 sdfsdfb", "dfg", "new" )

#4


0  

You could use the replace method;

你可以使用replace方法;

'This is %mystring. What is %yourstring?'.replace(/%mystring/g, '"%mystring')
// returns This is "%mystring. What is %yourstring?

EDIT: I updated my answer for completeness (Vegeta's answer looks like it solved your issue):

编辑:我更新了我的完整答案(Vegeta的答案看起来像是解决了你的问题):

var mystring = '%this is my string. what is your string?'
mystring.replace(/\%(\w+)/g, '\"%$1')

#1


1  

try this:

尝试这个:

str = 'go to this %link not this %link';
replacedStr = str.replace(/\s\%(.*?)(\s|$)/g, ' <span>%$1</span>$2');

#2


0  

please try the next code:

请尝试下一个代码:

var str = "my str is %something %somethingElse";
var newStr = str.replace(/%(\S+)/g, function(match, p1) {return '<a>' + p1 + '</a>'});

#3


0  

if there is no mandate to use regex :), then try

如果没有使用正则表达式的授权:),那么试试吧

var text = "sadfg dfgikwer sfd dfg3 dfg4 sdfsdfb";
text = text.split( " " ).map( function(value){ if ( value.indexOf( "dfg" ) != 0 ){ return value }else { return "new" } } ).join( " " );
alert(text);

this replaces a word that begins with dfg with new

这将替换以dfg开头的单词

making this a method

使这个方法

function replaceOldWithNew( text, oldStr, newStr )
{
  return text = text.split( " " ).map( function(value){ if ( value.indexOf( oldStr ) != 0 ){ return value }else { return newStr } } ).join( " " );
}

 replaceOldWithNew( "sadfg dfgikwer sfd dfg3 dfg4 sdfsdfb", "dfg", "new" )

#4


0  

You could use the replace method;

你可以使用replace方法;

'This is %mystring. What is %yourstring?'.replace(/%mystring/g, '"%mystring')
// returns This is "%mystring. What is %yourstring?

EDIT: I updated my answer for completeness (Vegeta's answer looks like it solved your issue):

编辑:我更新了我的完整答案(Vegeta的答案看起来像是解决了你的问题):

var mystring = '%this is my string. what is your string?'
mystring.replace(/\%(\w+)/g, '\"%$1')