te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>regexp replace - JavaScript Regex - Remove Whitespace from Start and End - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

regexp replace - JavaScript Regex - Remove Whitespace from Start and End - Stack Overflow

programmeradmin3浏览0评论

I worked on the below challenge for about 3 hours and none of my code was working. Decided to look at the solution to understand why I was not working. When I looked at the solution I was confused because I thought that \s to identify white spaces not to remove them... can someone give me hand and explain why the usage of \s instead of \S and why using the empty string ("") to get rid of the white spaces on both ends.

CHALLENGE

Write a regex and use the appropriate string methods to remove whitespace at the beginning and end of strings.

//SOLUTION

let hello = "   Hello, World!  ";
let wsRegex = /^\s+|\s+$/g; 
let result = hello.replace(wsRegex, "");

I worked on the below challenge for about 3 hours and none of my code was working. Decided to look at the solution to understand why I was not working. When I looked at the solution I was confused because I thought that \s to identify white spaces not to remove them... can someone give me hand and explain why the usage of \s instead of \S and why using the empty string ("") to get rid of the white spaces on both ends.

CHALLENGE

Write a regex and use the appropriate string methods to remove whitespace at the beginning and end of strings.

//SOLUTION

let hello = "   Hello, World!  ";
let wsRegex = /^\s+|\s+$/g; 
let result = hello.replace(wsRegex, "");
Share Improve this question edited Jun 16, 2021 at 23:10 PM 77-1 13.3k21 gold badges71 silver badges115 bronze badges asked Jun 16, 2021 at 23:07 Cristian M.Cristian M. 771 gold badge2 silver badges7 bronze badges 3
  • 1 The code identifies white space characters and then replaces them with empty string. By replacing something with nothing we get the same result as deleting something. – PM 77-1 Commented Jun 16, 2021 at 23:11
  • It's finding whitespace and replacing what it finds with an empty string, aka removing it – Andy Ray Commented Jun 16, 2021 at 23:12
  • 1 .... you decided to implement .trim() function – Amir Azizkhani Commented Feb 14, 2023 at 4:43
Add a ment  | 

3 Answers 3

Reset to default 12
  • \s means whitespace characters in regex, like space, tab, etc.
  • ^ means the beginning of the string
  • $ means the end of the string
  • | means OR (match the left side or the right side)
  • + means 1 or more (based off of the rule on the left)
  • /a regex/g the g means "global", aka "match multiple times" since you could need to match at the beginning AND end

So the regex means:

/^\s+|\s+$/g
/         /       Wrap the regex (how you do it in JS)
 ^\s+             Try to match at the beginning one or more whitespace chars
     |            Or...
      \s+$        Try to match whitespace chars at the end
           g      Match as many times as you can

String.prototype.replace replaces the match(es) found in the regex with the string provided as the 2nd argument, in this case an empty string.

So the process internally is:

  1. Look for all sections that match the regex (which will be the whitespace at the beginning and the whitespace at the end
  2. Replace each match with "", removing those matches entirely

let hello = "   Hello, World!  ";
let wsRegex = /^\s+|\s+$/g; 
let result = hello.replace(wsRegex, "");

console.log('"' + result + '"');

Most people use String.prototype.replaceAll instead of .replace when they use the global flag

let hello = "   Hello, World!  ";
let wsRegex = /^\s+|\s+$/g; 
let result = hello.replaceAll(wsRegex, "");

console.log('"' + result + '"');

The second argument of replace is for what you will replace from the match(es) of the first argument.

The regex will match/select the spaces on the beginning (^) and on the end ($) of the string, and then will be replaced by "".

When you use the regex /(\S)/g you're matching everything but spaces, in this case you will use something like hello.replace(/(\S)/g, '$1');

$1 means the first group of your regex.

let str = "__ @!Hello World___-!! "

console.log(str); //"__ @!Hello World___-!! "

console.log(str.replace(/^[^a-zA-Z0-9]*|[^a-zA-Z0-9]*$/g, '')); //"Hello World"

发布评论

评论列表(0)

  1. 暂无评论