如何在字符串中的单词末尾实现正则表达式 [英] How do I implement a regular expression at the end of a word in a string

查看:123
本文介绍了如何在字符串中的单词末尾实现正则表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要帮助才能在字符串上实现正则表达式。



就像我有一个像这样的字符串'safsdfsd $ sdfsdf $ fasdf $ sdfdzs $'它应该用#替换$。



我做了类似这样的工作



 x.replace(/ [$] / g,'#'); 





但我想要好像如果$出现在开头它应该用#替换它,如果$在一个单词的末尾,它应该替换为##



所以我的字符串应该从'fgsdfg $ sdfsdgf $ sdfas $ dgfdg $'变为'fgsdfg#sdfsdgf ## sdfas #dgfdg ##'



什么我试过了:



这是我试过的代码



 x.replace(/ [$] / g,'#'); 

解决方案

sdfsdf


fasdf

sdfdzs

I need help on implementing regex on a string.

Like if I have a string like this 'safsdfsd $sdfsdf$ fasdf $sdfdzs$' it should replace the $ with #.

I did something like this and it works

x.replace(/[$]/g,'#');



But I want it to be like if the $ is present at the beginning it should replace it with # and if the $ is at the end of a word it should replace with ##

So my string should go from 'fgsdfg $sdfsdgf$ sdfas $dgfdg$' to 'fgsdfg #sdfsdgf## sdfas #dgfdg##'

What I have tried:

Here's the code I tried

x.replace(/[$]/g,'#');

解决方案

sdfsdf


fasdf


sdfdzs


这篇关于如何在字符串中的单词末尾实现正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆