最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Replace Lessthan and character with Lessthan space and character in javascript - Stack Overflow

programmeradmin3浏览0评论

In my text box, i should find (<LessThan Symobl> <character>) and replace with (<LessThan Symbol <space> <character>)

Example:

Input:

abc<xyz abc <abc

output:

abc< xyz abc < abc

From a string, i should find ([LessThanSymbol] [character]) and replace with ([LessThanSymbol] [space] [Character] ) in javascript

In my text box, i should find (<LessThan Symobl> <character>) and replace with (<LessThan Symbol <space> <character>)

Example:

Input:

abc<xyz abc <abc

output:

abc< xyz abc < abc

From a string, i should find ([LessThanSymbol] [character]) and replace with ([LessThanSymbol] [space] [Character] ) in javascript

Share Improve this question edited Sep 9, 2011 at 14:30 Bill the Lizard 406k212 gold badges573 silver badges891 bronze badges asked Mar 3, 2011 at 20:54 SKumarSKumar 1,2376 gold badges19 silver badges30 bronze badges 4
  • I'm not sure I understand the desired result... – Brandon Frohbieter Commented Mar 3, 2011 at 20:56
  • Re write the question please! – JAiro Commented Mar 3, 2011 at 20:57
  • I have rewritten the question again below. I should probably need some regular expression that finds lessthan symbol followed by a character and replace with lessthan symbol followed by space followed by character. – SKumar Commented Mar 3, 2011 at 21:08
  • There is an edit button under the question. Do not rewrite the question and post it as an answer. – gen_Eric Commented Mar 3, 2011 at 21:45
Add a ment  | 

5 Answers 5

Reset to default 5

You can use a regular expression to find all < character that are followed by a non-space character, and insert a space:

s = s.replace(/<([^ ])/g, '< $1');
s = s.replace(/<(?=\S|$)/g, "< ");

What this means:

  • You can guess what .replace() does and what the < represents. :-)
  • (?=...) is a "positive look-ahead" — it means "followed by".
  • \S matches any non-whitespace character (anything other than spaces, non-breaking spaces, tabs, carriage returns, and newlines).
  • $ matches the end of the string.
  • /.../g performs a global search/replace.

Using the look-ahead prevents you from having to use a capturing group ($1), using \S fixes your newline problem, and using $ keeps you from having a similar problem if the string ends with a less-than.

>>> "<foo < bar <\tbaz <\rquux <\nquuux <".replace(/<(?=\S|$)/g, "< ")
"< foo < bar <\tbaz <\rquux <\nquuux <"

The best I've found is this :

s = s.replace(/<(?!\s)(?!$)/g,'< ');

It means:

  • (?!\s) - not followed by a whitespace (Negative Lookahead)
  • (?!$) - not followed by the end of the string (Negative Lookahead)

You can also use a split() then a join() :

'abc<xyz abc <abc'.split('<').join('< ')

But there is a problem with this method : the trail character which is pleted with a non necessary space.

'abc<xyz abc <abc<'.split('<').join('< ')

gives this result

'abc< xyz abc < abc< '

Try something like...

var string_variable;
string_variable = "Replace text using javascript replace function within a javascript string variable";
string_variable = string_variable.replace(/javascript/g, "js");
alert(string_variable);

(see: http://www.kodyaz./articles/replace-all-occurrences-of-string-using-javascript-replace-function.aspx)

I found answer for this.

s= s.replace(/<([^ \n])/g, '< $1');

It inserts space after lessthan for all the occurances of lessthan followed by (nonspace and non new line)

发布评论

评论列表(0)

  1. 暂无评论